|
| 1 | +package com.termux.api.settings.activities; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.os.Environment; |
| 6 | + |
| 7 | +import androidx.annotation.NonNull; |
| 8 | +import androidx.appcompat.app.AppCompatActivity; |
| 9 | +import androidx.preference.Preference; |
| 10 | +import androidx.preference.PreferenceFragmentCompat; |
| 11 | + |
| 12 | +import com.termux.api.R; |
| 13 | +import com.termux.shared.activities.ReportActivity; |
| 14 | +import com.termux.shared.file.FileUtils; |
| 15 | +import com.termux.shared.models.ReportInfo; |
| 16 | +import com.termux.shared.interact.ShareUtils; |
| 17 | +import com.termux.shared.android.PackageUtils; |
| 18 | +import com.termux.shared.termux.settings.preferences.TermuxAPIAppSharedPreferences; |
| 19 | +import com.termux.shared.android.AndroidUtils; |
| 20 | +import com.termux.shared.termux.TermuxConstants; |
| 21 | +import com.termux.shared.termux.TermuxUtils; |
| 22 | +import com.termux.shared.activity.media.AppCompatActivityUtils; |
| 23 | +import com.termux.shared.theme.NightMode; |
| 24 | + |
| 25 | +public class TermuxAPISettingsActivity extends AppCompatActivity { |
| 26 | + |
| 27 | + @Override |
| 28 | + protected void onCreate(Bundle savedInstanceState) { |
| 29 | + super.onCreate(savedInstanceState); |
| 30 | + |
| 31 | + AppCompatActivityUtils.setNightMode(this, NightMode.getAppNightMode().getName(), true); |
| 32 | + |
| 33 | + setContentView(R.layout.activity_termux_api_settings); |
| 34 | + if (savedInstanceState == null) { |
| 35 | + getSupportFragmentManager() |
| 36 | + .beginTransaction() |
| 37 | + .replace(R.id.settings, new RootPreferencesFragment()) |
| 38 | + .commit(); |
| 39 | + } |
| 40 | + |
| 41 | + AppCompatActivityUtils.setToolbar(this, com.termux.shared.R.id.toolbar); |
| 42 | + AppCompatActivityUtils.setShowBackButtonInActionBar(this, true); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public boolean onSupportNavigateUp() { |
| 47 | + onBackPressed(); |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + public static class RootPreferencesFragment extends PreferenceFragmentCompat { |
| 52 | + @Override |
| 53 | + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { |
| 54 | + Context context = getContext(); |
| 55 | + if (context == null) return; |
| 56 | + |
| 57 | + setPreferencesFromResource(R.xml.sets__termux, rootKey); |
| 58 | + |
| 59 | + new Thread() { |
| 60 | + @Override |
| 61 | + public void run() { |
| 62 | + configureTermuxAPIPreference(context); |
| 63 | + configureAboutPreference(context); |
| 64 | + configureDonatePreference(context); |
| 65 | + } |
| 66 | + }.start(); |
| 67 | + } |
| 68 | + |
| 69 | + private void configureTermuxAPIPreference(@NonNull Context context) { |
| 70 | + Preference termuxAPIPreference = findPreference("sets__termux_api_app"); |
| 71 | + if (termuxAPIPreference != null) { |
| 72 | + TermuxAPIAppSharedPreferences preferences = TermuxAPIAppSharedPreferences.build(context, false); |
| 73 | + // If failed to get app preferences, then likely app is not installed, so do not show its preference |
| 74 | + termuxAPIPreference.setVisible(preferences != null); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private void configureAboutPreference(@NonNull Context context) { |
| 79 | + Preference aboutPreference = findPreference("link__termux_about"); |
| 80 | + if (aboutPreference != null) { |
| 81 | + aboutPreference.setOnPreferenceClickListener(preference -> { |
| 82 | + new Thread() { |
| 83 | + @Override |
| 84 | + public void run() { |
| 85 | + String title = "About"; |
| 86 | + |
| 87 | + StringBuilder aboutString = new StringBuilder(); |
| 88 | + aboutString.append(TermuxUtils.getAppInfoMarkdownString(context, TermuxUtils.AppInfoMode.TERMUX_AND_PLUGIN_PACKAGE)); |
| 89 | + aboutString.append("\n\n").append(AndroidUtils.getDeviceInfoMarkdownString(context, true)); |
| 90 | + aboutString.append("\n\n").append(TermuxUtils.getImportantLinksMarkdownString(context)); |
| 91 | + |
| 92 | + ReportInfo reportInfo = new ReportInfo(title, |
| 93 | + TermuxConstants.TERMUX_API_APP.TERMUX_API_MAIN_ACTIVITY_NAME, title); |
| 94 | + reportInfo.setReportString(aboutString.toString()); |
| 95 | + reportInfo.setReportSaveFileLabelAndPath(title, |
| 96 | + Environment.getExternalStorageDirectory() + "/" + |
| 97 | + FileUtils.sanitizeFileName(TermuxConstants.TERMUX_API_APP_NAME.replaceAll(":", "") + |
| 98 | + "-" + title + ".log", true, true)); |
| 99 | + |
| 100 | + ReportActivity.startReportActivity(context, reportInfo); |
| 101 | + } |
| 102 | + }.start(); |
| 103 | + |
| 104 | + return true; |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private void configureDonatePreference(@NonNull Context context) { |
| 110 | + Preference donatePreference = findPreference("link__termux_donate"); |
| 111 | + if (donatePreference != null) { |
| 112 | + String signingCertificateSHA256Digest = PackageUtils.getSigningCertificateSHA256DigestForPackage(context); |
| 113 | + if (signingCertificateSHA256Digest != null) { |
| 114 | + // If APK is a Google Playstore release, then do not show the donation link |
| 115 | + // since Termux isn't exempted from the playstore policy donation links restriction |
| 116 | + // Check Fund solicitations: https://pay.google.com/intl/en_in/about/policy/ |
| 117 | + String apkRelease = TermuxUtils.getAPKRelease(signingCertificateSHA256Digest); |
| 118 | + if (apkRelease == null || apkRelease.equals(TermuxConstants.APK_RELEASE_GOOGLE_PLAYSTORE_SIGNING_CERTIFICATE_SHA256_DIGEST)) { |
| 119 | + donatePreference.setVisible(false); |
| 120 | + return; |
| 121 | + } else { |
| 122 | + donatePreference.setVisible(true); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + donatePreference.setOnPreferenceClickListener(preference -> { |
| 127 | + ShareUtils.openUrl(context, TermuxConstants.TERMUX_DONATE_URL); |
| 128 | + return true; |
| 129 | + }); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments