]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/AboutActivity.java
Redesign file access to work with the scoped storage. https://redmine.stoutner.com...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / AboutActivity.java
index 3d5170dfc97d9e60410f3ade3a22942f58e8317d..31e9c152f5d7e71338a5812d6fe13720d909cead 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2016-2020 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2016-2021 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
 
 package com.stoutner.privacybrowser.activities;
 
+import android.app.Activity;
+import android.app.Dialog;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.net.Uri;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
 import android.view.WindowManager;
+import android.widget.EditText;
+import android.widget.LinearLayout;
 
 import androidx.appcompat.app.ActionBar;
 import androidx.appcompat.app.AppCompatActivity;
 import androidx.appcompat.widget.Toolbar;
+import androidx.fragment.app.DialogFragment;
 import androidx.viewpager.widget.ViewPager;
 
+import com.google.android.material.snackbar.Snackbar;
 import com.google.android.material.tabs.TabLayout;
 
 import com.stoutner.privacybrowser.adapters.AboutPagerAdapter;
 import com.stoutner.privacybrowser.R;
+import com.stoutner.privacybrowser.asynctasks.SaveAboutVersionImage;
+import com.stoutner.privacybrowser.dialogs.SaveDialog;
+import com.stoutner.privacybrowser.fragments.AboutVersionFragment;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+
+public class AboutActivity extends AppCompatActivity implements SaveDialog.SaveListener {
+    // Declare the class variables.
+    private AboutPagerAdapter aboutPagerAdapter;
 
-public class AboutActivity extends AppCompatActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         // Get a handle for the shared preferences.
@@ -61,6 +83,9 @@ public class AboutActivity extends AppCompatActivity {
         // Store the blocklist versions.
         String[] blocklistVersions = launchingIntent.getStringArrayExtra("blocklist_versions");
 
+        // Remove the incorrect lint warning below that the blocklist versions might be null.
+        assert blocklistVersions != null;
+
         // Set the content view.
         setContentView(R.layout.about_coordinatorlayout);
 
@@ -81,10 +106,128 @@ public class AboutActivity extends AppCompatActivity {
         // Display the home arrow on action bar.
         actionBar.setDisplayHomeAsUpEnabled(true);
 
-        //  Setup the ViewPager.
-        aboutViewPager.setAdapter(new AboutPagerAdapter(getSupportFragmentManager(), getApplicationContext(), blocklistVersions));
+        // Initialize the about pager adapter.
+        aboutPagerAdapter = new AboutPagerAdapter(getSupportFragmentManager(), getApplicationContext(), blocklistVersions);
+
+        // Setup the ViewPager.
+        aboutViewPager.setAdapter(aboutPagerAdapter);
+
+        // Keep all the tabs in memory.  This prevents the memory usage updater from running multiple times.
+        aboutViewPager.setOffscreenPageLimit(10);
 
         // Connect the tab layout to the view pager.
         aboutTabLayout.setupWithViewPager(aboutViewPager);
     }
+
+    // The activity result is called after browsing for a file in the save alert dialog.
+    @Override
+    public void onActivityResult(int requestCode, int resultCode, Intent returnedIntent) {
+        // Run the default commands.
+        super.onActivityResult(requestCode, resultCode, returnedIntent);
+
+        // Only do something if the user didn't press back from the file picker.
+        if (resultCode == Activity.RESULT_OK) {
+            // Get a handle for the save dialog fragment.
+            DialogFragment saveDialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.save_dialog));
+
+            // Only update the file name if the dialog still exists.
+            if (saveDialogFragment != null) {
+                // Get a handle for the save dialog.
+                Dialog saveDialog = saveDialogFragment.getDialog();
+
+                // Remove the lint warning below that the dialog might be null.
+                assert saveDialog != null;
+
+                // Get a handle for the dialog view.
+                EditText fileNameEditText = saveDialog.findViewById(R.id.file_name_edittext);
+
+                // Get the file name URI from the intent.
+                Uri fileNameUri = returnedIntent.getData();
+
+                // Get the file name string from the URI.
+                String fileNameString = fileNameUri.toString();
+
+                // Set the file name text.
+                fileNameEditText.setText(fileNameString);
+
+                // Move the cursor to the end of the file name edit text.
+                fileNameEditText.setSelection(fileNameString.length());
+            }
+        }
+    }
+
+    @Override
+    public void onSave(int saveType, DialogFragment dialogFragment) {
+        // Get a handle for the dialog.
+        Dialog dialog = dialogFragment.getDialog();
+
+        // Remove the lint warning below that the dialog might be null.
+        assert dialog != null;
+
+        // Get a handle for the file name edit text.
+        EditText fileNameEditText = dialog.findViewById(R.id.file_name_edittext);
+
+        // Get the file name string.
+        String fileNameString = fileNameEditText.getText().toString();
+
+        // Get a handle for the about version linear layout.
+        LinearLayout aboutVersionLinearLayout = findViewById(R.id.about_version_linearlayout);
+
+        // Save the file according to the type.
+        switch (saveType) {
+            case SaveDialog.SAVE_ABOUT_VERSION_TEXT:
+                try {
+                    // Get a handle for the about version fragment.
+                    AboutVersionFragment aboutVersionFragment = (AboutVersionFragment) aboutPagerAdapter.getTabFragment(0);
+
+                    // Get the about version text.
+                    String aboutVersionString = aboutVersionFragment.getAboutVersionString();
+
+                    // Create an input stream with the contents of about version.
+                    InputStream aboutVersionInputStream = new ByteArrayInputStream(aboutVersionString.getBytes(StandardCharsets.UTF_8));
+
+                    // Create an about version buffered reader.
+                    BufferedReader aboutVersionBufferedReader = new BufferedReader(new InputStreamReader(aboutVersionInputStream));
+
+                    // Open an output stream.
+                    OutputStream outputStream = getContentResolver().openOutputStream(Uri.parse(fileNameString));
+
+                    // Create a file buffered writer.
+                    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
+
+                    // Create a transfer string.
+                    String transferString;
+
+                    // Use the transfer string to copy the about version text from the buffered reader to the buffered writer.
+                    while ((transferString = aboutVersionBufferedReader.readLine()) != null) {
+                        // Append the line to the buffered writer.
+                        bufferedWriter.append(transferString);
+
+                        // Append a line break.
+                        bufferedWriter.append("\n");
+                    }
+
+                    // Flush the buffered writer.
+                    bufferedWriter.flush();
+
+                    // Close the inputs and outputs.
+                    aboutVersionBufferedReader.close();
+                    aboutVersionInputStream.close();
+                    bufferedWriter.close();
+                    outputStream.close();
+
+                    // Display a snackbar with the saved about version information.
+                    Snackbar.make(aboutVersionLinearLayout, getString(R.string.file_saved) + "  " + fileNameString, Snackbar.LENGTH_SHORT).show();
+                } catch (Exception exception) {
+                    // Display a snackbar with the error message.
+                    Snackbar.make(aboutVersionLinearLayout, getString(R.string.error_saving_file) + "  " + exception.toString(), Snackbar.LENGTH_INDEFINITE).show();
+                }
+                break;
+
+            case SaveDialog.SAVE_ABOUT_VERSION_IMAGE:
+                // Save the about version image.
+                new SaveAboutVersionImage(this, fileNameString, aboutVersionLinearLayout).execute();
+                break;
+        }
+    }
 }
\ No newline at end of file