X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FAboutActivity.java;h=94801fb9b5579a091e0a88476daf4d027544db96;hp=c20452151e9f5c010795593bdd577fdb07384eba;hb=641d15ace34579762580ed8297f324133354499b;hpb=74857298f431569df1849e8ef91387cb25274095 diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/AboutActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/AboutActivity.java index c2045215..94801fb9 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/AboutActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/AboutActivity.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2017 Soren Stoutner . + * Copyright © 2016-2021 Soren Stoutner . * * This file is part of Privacy Browser . * @@ -19,98 +19,215 @@ 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.support.design.widget.TabLayout; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; -import android.support.v4.view.ViewPager; -import android.support.v7.app.ActionBar; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; - -import com.stoutner.privacybrowser.fragments.AboutTabFragment; +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) { - // Set the theme. - if (MainWebViewActivity.darkTheme) { - setTheme(R.style.PrivacyBrowserDark_SecondaryActivity); - } else { - setTheme(R.style.PrivacyBrowserLight_SecondaryActivity); + // Get a handle for the shared preferences. + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + + // Get the screenshot preference. + boolean allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false); + + // Disable screenshots if not allowed. + if (!allowScreenshots) { + getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); } + // Set the theme. + setTheme(R.style.PrivacyBrowser); + // Run the default commands. super.onCreate(savedInstanceState); + // Get the intent that launched the activity. + Intent launchingIntent = getIntent(); + + // 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); - // We need to use the SupportActionBar from android.support.v7.app.ActionBar until the minimum API is >= 21. - Toolbar aboutAppBar = (Toolbar) findViewById(R.id.about_toolbar); - setSupportActionBar(aboutAppBar); + // Get handles for the views. + Toolbar toolbar = findViewById(R.id.about_toolbar); + TabLayout aboutTabLayout = findViewById(R.id.about_tablayout); + ViewPager aboutViewPager = findViewById(R.id.about_viewpager); + + // Set the action bar. `SupportActionBar` must be used until the minimum API is >= 21. + setSupportActionBar(toolbar); + + // Get a handle for the action bar. + final ActionBar actionBar = getSupportActionBar(); + + // Remove the incorrect lint warning that the action bar might be null. + assert actionBar != null; // + + // Display the home arrow on action bar. + actionBar.setDisplayHomeAsUpEnabled(true); - // Display the home arrow on supportAppBar. - final ActionBar appBar = getSupportActionBar(); - assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that appBar might be null. - appBar.setDisplayHomeAsUpEnabled(true); + // Initialize the about pager adapter. + aboutPagerAdapter = new AboutPagerAdapter(getSupportFragmentManager(), getApplicationContext(), blocklistVersions); - // Setup the ViewPager. - ViewPager aboutViewPager = (ViewPager) findViewById(R.id.about_viewpager); - aboutViewPager.setAdapter(new aboutPagerAdapter(getSupportFragmentManager())); + // Setup the ViewPager. + aboutViewPager.setAdapter(aboutPagerAdapter); - // Setup the TabLayout and connect it to the ViewPager. - TabLayout aboutTabLayout = (TabLayout) findViewById(R.id.about_tablayout); + // 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); } - private class aboutPagerAdapter extends FragmentPagerAdapter { - private aboutPagerAdapter(FragmentManager fm) { - super(fm); - } - - @Override - // Get the count of the number of tabs. - public int getCount() { - return 7; - } + // 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); - @Override - // Get the name of each tab. Tab numbers start at 0. - public CharSequence getPageTitle(int tab) { - switch (tab) { - case 0: - return getString(R.string.version); + // 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)); - case 1: - return getString(R.string.permissions); + // Only update the file name if the dialog still exists. + if (saveDialogFragment != null) { + // Get a handle for the save dialog. + Dialog saveDialog = saveDialogFragment.getDialog(); - case 2: - return getString(R.string.privacy_policy); + // Remove the lint warning below that the dialog might be null. + assert saveDialog != null; - case 3: - return getString(R.string.changelog); + // Get a handle for the dialog view. + EditText fileNameEditText = saveDialog.findViewById(R.id.file_name_edittext); - case 4: - return getString(R.string.licenses); + // Get the file name URI from the intent. + Uri fileNameUri = returnedIntent.getData(); - case 5: - return getString(R.string.contributors); + // Get the file name string from the URI. + String fileNameString = fileNameUri.toString(); - case 6: - return getString(R.string.links); + // Set the file name text. + fileNameEditText.setText(fileNameString); - default: - return ""; + // 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; - @Override - // Setup each tab. - public Fragment getItem(int tab) { - return AboutTabFragment.createTab(tab); + case SaveDialog.SAVE_ABOUT_VERSION_IMAGE: + // Save the about version image. + new SaveAboutVersionImage(this, fileNameString, aboutVersionLinearLayout).execute(); + break; } } -} +} \ No newline at end of file