add expert preference to enable Popularity Contest

closes #396
This commit is contained in:
Hans-Christoph Steiner 2021-02-24 22:55:33 +01:00
parent d1e80bb067
commit a207798f5c
8 changed files with 152 additions and 28 deletions

View File

@ -153,6 +153,12 @@
android:summary="@string/keep_install_history_summary"
android:defaultValue="false"
android:dependency="expert"/>
<CheckBoxPreference
android:key="sendToFdroidMetrics"
android:title="@string/send_to_fdroid_metrics"
android:summary="@string/send_to_fdroid_metrics_summary"
android:defaultValue="false"
android:dependency="expert"/>
<CheckBoxPreference
android:key="hideAllNotifications"
android:title="@string/hide_all_notifications"

View File

@ -28,11 +28,10 @@ import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
import android.text.format.DateUtils;
import android.util.Log;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
import org.fdroid.fdroid.installer.PrivilegedInstaller;
import org.fdroid.fdroid.net.ConnectivityMonitorService;
@ -95,6 +94,7 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
public static final String PREF_KEEP_CACHE_TIME = "keepCacheFor";
public static final String PREF_UNSTABLE_UPDATES = "unstableUpdates";
public static final String PREF_KEEP_INSTALL_HISTORY = "keepInstallHistory";
public static final String PREF_SEND_TO_FDROID_METRICS = "sendToFdroidMetrics";
public static final String PREF_EXPERT = "expert";
public static final String PREF_FORCE_OLD_INDEX = "forceOldIndex";
public static final String PREF_PRIVILEGED_INSTALLER = "privilegedInstaller";
@ -363,6 +363,10 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
return preferences.getBoolean(PREF_KEEP_INSTALL_HISTORY, IGNORED_B);
}
public boolean isSendingToFDroidMetrics() {
return isKeepingInstallHistory() && preferences.getBoolean(PREF_SEND_TO_FDROID_METRICS, IGNORED_B);
}
public boolean showIncompatibleVersions() {
return preferences.getBoolean(PREF_SHOW_INCOMPAT_VERSIONS, IGNORED_B);
}

View File

@ -25,17 +25,19 @@ import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import androidx.core.app.ShareCompat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ShareCompat;
import org.apache.commons.io.IOUtils;
import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.R;
import org.fdroid.fdroid.data.Repo;
import org.fdroid.fdroid.data.RepoProvider;
import org.fdroid.fdroid.installer.InstallHistoryService;
import org.fdroid.fdroid.work.FDroidMetricsWorker;
import java.io.FileDescriptor;
import java.io.FileInputStream;
@ -45,15 +47,28 @@ import java.nio.charset.Charset;
public class InstallHistoryActivity extends AppCompatActivity {
public static final String TAG = "InstallHistoryActivity";
private boolean showingInstallHistory;
private Toolbar toolbar;
private MenuItem showMenuItem;
private TextView textView;
private String appName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_install_history);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(getString(R.string.install_history));
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
textView = findViewById(R.id.text);
appName = getString(R.string.app_name);
showInstallHistory();
}
private void showInstallHistory() {
String text = "";
try {
ContentResolver resolver = getContentResolver();
@ -71,13 +86,35 @@ public class InstallHistoryActivity extends AppCompatActivity {
} catch (IOException | SecurityException | IllegalStateException e) {
e.printStackTrace();
}
TextView textView = findViewById(R.id.text);
toolbar.setTitle(getString(R.string.install_history));
textView.setText(text);
showingInstallHistory = true;
if (showMenuItem != null) {
showMenuItem.setVisible(Preferences.get().isSendingToFDroidMetrics());
showMenuItem.setTitle(R.string.menu_show_fdroid_metrics_report);
}
}
private void showFDroidMetricsReport() {
toolbar.setTitle(getString(R.string.fdroid_metrics_report, appName));
textView.setText(FDroidMetricsWorker.generateReport(this));
showingInstallHistory = false;
if (showMenuItem != null) {
showMenuItem.setVisible(Preferences.get().isSendingToFDroidMetrics());
showMenuItem.setTitle(R.string.menu_show_install_history);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.install_history, menu);
showMenuItem = menu.findItem(R.id.menu_show);
showMenuItem.setVisible(Preferences.get().isSendingToFDroidMetrics());
if (showingInstallHistory) {
showMenuItem.setTitle(R.string.menu_show_fdroid_metrics_report);
} else {
showMenuItem.setTitle(R.string.menu_show_install_history);
}
return super.onCreateOptionsMenu(menu);
}
@ -86,30 +123,47 @@ public class InstallHistoryActivity extends AppCompatActivity {
switch (item.getItemId()) {
case R.id.menu_share:
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Repos:\n");
for (Repo repo : RepoProvider.Helper.all(this)) {
if (repo.inuse) {
stringBuilder.append("* ");
stringBuilder.append(repo.address);
stringBuilder.append('\n');
ShareCompat.IntentBuilder intentBuilder = ShareCompat.IntentBuilder.from(this);
if (showingInstallHistory) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Repos:\n");
for (Repo repo : RepoProvider.Helper.all(this)) {
if (repo.inuse) {
stringBuilder.append("* ");
stringBuilder.append(repo.address);
stringBuilder.append('\n');
}
}
intentBuilder
.setText(stringBuilder.toString())
.setStream(InstallHistoryService.LOG_URI)
.setType("text/plain")
.setSubject(getString(R.string.send_history_csv, appName))
.setChooserTitle(R.string.send_install_history);
} else {
intentBuilder
.setText(textView.getText())
.setType("application/json")
.setSubject(getString(R.string.send_fdroid_metrics_json, appName))
.setChooserTitle(R.string.send_fdroid_metrics_report);
}
ShareCompat.IntentBuilder intentBuilder = ShareCompat.IntentBuilder.from(this)
.setStream(InstallHistoryService.LOG_URI)
.setSubject(getString(R.string.send_history_csv, getString(R.string.app_name)))
.setChooserTitle(R.string.send_install_history)
.setText(stringBuilder.toString())
.setType("text/plain");
Intent intent = intentBuilder.getIntent();
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
break;
case R.id.menu_delete:
getContentResolver().delete(InstallHistoryService.LOG_URI, null, null);
TextView textView = findViewById(R.id.text);
if (showingInstallHistory) {
getContentResolver().delete(InstallHistoryService.LOG_URI, null, null);
}
textView.setText("");
break;
case R.id.menu_show:
if (showingInstallHistory) {
showFDroidMetricsReport();
} else {
showInstallHistory();
}
break;
}
return super.onOptionsItemSelected(item);
}

View File

@ -34,7 +34,7 @@ import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.CheckBoxPreference;
import androidx.preference.EditTextPreference;
@ -47,7 +47,7 @@ import androidx.preference.SeekBarPreference;
import androidx.preference.SwitchPreference;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;
import info.guardianproject.netcipher.proxy.OrbotHelper;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.Languages;
import org.fdroid.fdroid.Preferences;
@ -58,8 +58,7 @@ import org.fdroid.fdroid.data.RepoProvider;
import org.fdroid.fdroid.installer.InstallHistoryService;
import org.fdroid.fdroid.installer.PrivilegedInstaller;
import org.fdroid.fdroid.work.CleanCacheWorker;
import info.guardianproject.netcipher.proxy.OrbotHelper;
import org.fdroid.fdroid.work.FDroidMetricsWorker;
public class PreferencesFragment extends PreferenceFragmentCompat
implements SharedPreferences.OnSharedPreferenceChangeListener {
@ -104,6 +103,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat
private SwitchPreference useTorCheckPref;
private Preference updateAutoDownloadPref;
private CheckBoxPreference keepInstallHistoryPref;
private CheckBoxPreference sendToFDroidMetricsPref;
private Preference installHistoryPref;
private long currentKeepCacheTime;
private int overWifiPrevious;
@ -115,14 +115,22 @@ public class PreferencesFragment extends PreferenceFragmentCompat
@Override
public void onCreatePreferences(Bundle bundle, String s) {
Preferences.get().migrateOldPreferences();
Preferences preferences = Preferences.get();
preferences.migrateOldPreferences();
addPreferencesFromResource(R.xml.preferences);
otherPrefGroup = (PreferenceGroup) findPreference("pref_category_other");
keepInstallHistoryPref = (CheckBoxPreference) findPreference(Preferences.PREF_KEEP_INSTALL_HISTORY);
sendToFDroidMetricsPref = findPreference(Preferences.PREF_SEND_TO_FDROID_METRICS);
sendToFDroidMetricsPref.setEnabled(keepInstallHistoryPref.isChecked());
installHistoryPref = findPreference("installHistory");
installHistoryPref.setVisible(keepInstallHistoryPref.isChecked());
if (preferences.isSendingToFDroidMetrics()) {
installHistoryPref.setTitle(R.string.install_history_and_metrics);
} else {
installHistoryPref.setTitle(R.string.install_history);
}
useTorCheckPref = (SwitchPreference) findPreference(Preferences.PREF_USE_TOR);
useTorCheckPref.setOnPreferenceChangeListener(useTorChangedListener);
@ -369,11 +377,26 @@ public class PreferencesFragment extends PreferenceFragmentCompat
if (keepInstallHistoryPref.isChecked()) {
InstallHistoryService.register(getActivity());
installHistoryPref.setVisible(true);
sendToFDroidMetricsPref.setEnabled(true);
} else {
InstallHistoryService.unregister(getActivity());
installHistoryPref.setVisible(false);
sendToFDroidMetricsPref.setEnabled(false);
}
setFDroidMetricsWorker();
break;
case Preferences.PREF_SEND_TO_FDROID_METRICS:
setFDroidMetricsWorker();
break;
}
}
private void setFDroidMetricsWorker() {
if (sendToFDroidMetricsPref.isEnabled() && sendToFDroidMetricsPref.isChecked()) {
FDroidMetricsWorker.schedule(getContext());
} else {
FDroidMetricsWorker.cancel(getContext());
}
}
@ -526,6 +549,15 @@ public class PreferencesFragment extends PreferenceFragmentCompat
} else {
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
}
} else if (Preferences.PREF_SEND_TO_FDROID_METRICS.equals(key)) {
if (Preferences.get().isSendingToFDroidMetrics()) {
String msg = getString(R.string.toast_metrics_in_install_history,
getContext().getString(R.string.app_name));
Toast.makeText(getContext(), msg, Toast.LENGTH_LONG).show();
installHistoryPref.setTitle(R.string.install_history_and_metrics);
} else {
installHistoryPref.setTitle(R.string.install_history);
}
}
}
}

View File

@ -115,6 +115,10 @@ public class FDroidMetricsWorker extends Worker {
Utils.debugLog(TAG, "Scheduled periodic work");
}
public static void cancel(final Context context) {
WorkManager.getInstance(context).cancelUniqueWork(TAG);
}
@NonNull
@Override
public Result doWork() {

View File

@ -6,6 +6,11 @@
android:icon="@drawable/ic_share"
android:title="@string/menu_share"
app:showAsAction="ifRoom" />
<item
android:id="@+id/menu_show"
android:visible="false"
android:icon="@drawable/ic_delete"
android:title="@string/menu_show_fdroid_metrics_report" />
<item
android:id="@+id/menu_delete"
android:icon="@drawable/ic_delete"

View File

@ -28,11 +28,19 @@
drawer.
</string>
<string name="send_install_history">Send Install History</string>
<string name="send_fdroid_metrics_report">Send %s Metrics Report</string>
<string name="send_history_csv">%s install history as CSV file</string>
<string name="send_fdroid_metrics_json">%s metrics report as JSON file</string>
<string name="install_history">Install history</string>
<string name="install_history_and_metrics">Install history and metrics</string>
<string name="fdroid_metrics_report">%s Metrics Report</string>
<string name="install_history_summary">View the private log of all installs and uninstalls</string>
<string name="keep_install_history">Keep install history</string>
<string name="keep_install_history_summary">Store a log of all installs and uninstalls in a private store</string>
<string name="send_to_fdroid_metrics">Send usage data</string>
<string name="send_to_fdroid_metrics_summary">Sends anonymous data weekly to F-Droid Metrics (requires "Keep install history")</string>
<!-- message shown as a "toast" when the user enables the Send to F-Droid Metrics preference -->
<string name="toast_metrics_in_install_history">The %s Metric report is viewable in the Install History viewer</string>
<string name="send_version_and_uuid">Send version and UUID to servers</string>
<string name="send_version_and_uuid_summary">Include this app\'s version and a random, unique ID when
downloading, takes affect next app restart.</string>
@ -245,6 +253,11 @@ This often occurs with apps installed via Google Play or other sources, if they
<!-- The bottom bar button label. The updates tab isn't only about software updates. It is also about queued offline installs, in progress installs, known vulnerabilities, and in the future, also donation reminders and other things. In English, you can also say "do you have an update on your progress?" or "Keep me updated on how things are going". That combined with "software updates" is why the tab is called "Updates". Ideally, the Updates Tab would actually be a different word than the word used for "software update" to highlight that the Updates Tab is more than just software updates. -->
<string name="main_menu__updates">Updates</string>
<!-- Used to switch the Install History viewer to showing the FDroid Metrics report -->
<string name="menu_show_fdroid_metrics_report">Show metrics report</string>
<!-- Used to switch the FDroid Metrics report to showing the Install History viewer -->
<string name="menu_show_install_history">Show install history</string>
<string name="latest__empty_state__no_recent_apps">No recent apps found</string>
<string name="latest__empty_state__never_updated">Once your list of apps has been updated, the latest apps should
show here

View File

@ -179,6 +179,12 @@
android:summary="@string/keep_install_history_summary"
android:defaultValue="false"
android:dependency="expert"/>
<CheckBoxPreference
android:key="sendToFdroidMetrics"
android:title="@string/send_to_fdroid_metrics"
android:summary="@string/send_to_fdroid_metrics_summary"
android:defaultValue="false"
android:dependency="expert"/>
<CheckBoxPreference
android:key="hideAllNotifications"
android:title="@string/hide_all_notifications"