Search in sources :

Example 1 with DisposableMaybeObserver

use of io.reactivex.rxjava3.observers.DisposableMaybeObserver in project quran_android by quran.

the class QuranAdvancedSettingsFragment method onCreatePreferences.

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    addPreferencesFromResource(R.xml.quran_advanced_preferences);
    final Context context = requireActivity();
    appContext = context.getApplicationContext();
    // field injection
    ((QuranApplication) appContext).getApplicationComponent().inject(this);
    final Preference logsPref = findPreference(Constants.PREF_LOGS);
    if (BuildConfig.DEBUG || "beta".equals(BuildConfig.BUILD_TYPE)) {
        logsPref.setOnPreferenceClickListener(preference -> {
            if (logsSubscription == null) {
                logsSubscription = Observable.fromIterable(Timber.forest()).filter(tree -> tree instanceof RecordingLogTree).firstElement().map(tree -> ((RecordingLogTree) tree).getLogs()).map(logs -> QuranUtils.getDebugInfo(appContext, quranScreenInfo) + "\n\n" + logs).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableMaybeObserver<String>() {

                    @Override
                    public void onSuccess(@NonNull String logs) {
                        Intent intent = new Intent(Intent.ACTION_SEND);
                        intent.setType("message/rfc822");
                        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { appContext.getString(R.string.logs_email) });
                        intent.putExtra(Intent.EXTRA_TEXT, logs);
                        intent.putExtra(Intent.EXTRA_SUBJECT, "Logs");
                        startActivity(Intent.createChooser(intent, appContext.getString(R.string.prefs_send_logs_title)));
                        logsSubscription = null;
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
            }
            return true;
        });
    } else {
        removeAdvancePreference(logsPref);
    }
    final Preference importPref = findPreference(Constants.PREF_IMPORT);
    importPref.setOnPreferenceClickListener(preference -> {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        String[] mimeTypes = new String[] { "application/*", "text/*" };
        intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
        startActivityForResult(intent, REQUEST_CODE_IMPORT);
        return true;
    });
    final Preference exportPref = findPreference(Constants.PREF_EXPORT);
    exportPref.setOnPreferenceClickListener(preference -> {
        if (exportSubscription == null) {
            exportSubscription = bookmarkImportExportModel.exportBookmarksObservable().observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSingleObserver<Uri>() {

                @Override
                public void onSuccess(@NonNull Uri uri) {
                    Intent shareIntent = new Intent(Intent.ACTION_SEND);
                    shareIntent.setType("application/json");
                    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                    List<ResolveInfo> intents = appContext.getPackageManager().queryIntentActivities(shareIntent, 0);
                    if (intents.size() > 1) {
                        // if only one, then that is likely Quran for Android itself, so don't show
                        // the chooser since it doesn't really make sense.
                        context.startActivity(Intent.createChooser(shareIntent, context.getString(R.string.prefs_export_title)));
                    } else {
                        File exportedPath = new File(appContext.getExternalFilesDir(null), "backups");
                        String exported = appContext.getString(R.string.exported_data, exportedPath.toString());
                        ToastCompat.makeText(appContext, exported, Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onError(@NonNull Throwable e) {
                    exportSubscription = null;
                    if (isAdded()) {
                        ToastCompat.makeText(context, R.string.export_data_error, Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
        return true;
    });
    internalSdcardLocation = Environment.getExternalStorageDirectory().getAbsolutePath();
    listStoragePref = findPreference(getString(R.string.prefs_app_location));
    listStoragePref.setEnabled(false);
    try {
        storageList = StorageUtils.getAllStorageLocations(context.getApplicationContext());
    } catch (Exception e) {
        Timber.d(e, "Exception while trying to get storage locations");
        storageList = new ArrayList<>();
    }
    // except for the normal Environment.getExternalStorageDirectory
    if (storageList.size() <= 1) {
        Timber.d("removing advanced settings from preferences");
        hideStorageListPref();
    } else {
        loadStorageOptionsTask = new LoadStorageOptionsTask(context, quranFileUtils);
        loadStorageOptionsTask.execute();
    }
}
Also used : Context(android.content.Context) PreferenceGroup(androidx.preference.PreferenceGroup) Context(android.content.Context) QuranImportActivity(com.quran.labs.androidquran.QuranImportActivity) Bundle(android.os.Bundle) AlertDialog(androidx.appcompat.app.AlertDialog) Environment(android.os.Environment) NonNull(androidx.annotation.NonNull) Constants(com.quran.labs.androidquran.data.Constants) Uri(android.net.Uri) DataListPreference(com.quran.labs.androidquran.ui.preference.DataListPreference) Intent(android.content.Intent) QuranUtils(com.quran.labs.androidquran.util.QuranUtils) HashMap(java.util.HashMap) PreferenceFragmentCompat(androidx.preference.PreferenceFragmentCompat) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) Observable(io.reactivex.rxjava3.core.Observable) Toast(android.widget.Toast) R(com.quran.labs.androidquran.R) BuildConfig(com.quran.labs.androidquran.BuildConfig) QuranFileUtils(com.quran.labs.androidquran.util.QuranFileUtils) QuranSettings(com.quran.labs.androidquran.util.QuranSettings) Build(android.os.Build) AsyncTask(android.os.AsyncTask) QuranAdvancedPreferenceActivity(com.quran.labs.androidquran.QuranAdvancedPreferenceActivity) ProgressDialog(android.app.ProgressDialog) TextUtils(android.text.TextUtils) QuranApplication(com.quran.labs.androidquran.QuranApplication) PermissionUtil(com.quran.labs.androidquran.service.util.PermissionUtil) QuranScreenInfo(com.quran.labs.androidquran.util.QuranScreenInfo) Preference(androidx.preference.Preference) ToastCompat(com.quran.labs.androidquran.ui.util.ToastCompat) File(java.io.File) Timber(timber.log.Timber) ResolveInfo(android.content.pm.ResolveInfo) StorageUtils(com.quran.labs.androidquran.util.StorageUtils) AndroidSchedulers(io.reactivex.rxjava3.android.schedulers.AndroidSchedulers) StringRes(androidx.annotation.StringRes) List(java.util.List) DisposableSingleObserver(io.reactivex.rxjava3.observers.DisposableSingleObserver) BookmarkImportExportModel(com.quran.labs.androidquran.model.bookmark.BookmarkImportExportModel) Disposable(io.reactivex.rxjava3.disposables.Disposable) Activity(android.app.Activity) DisposableMaybeObserver(io.reactivex.rxjava3.observers.DisposableMaybeObserver) RecordingLogTree(com.quran.labs.androidquran.util.RecordingLogTree) DisposableSingleObserver(io.reactivex.rxjava3.observers.DisposableSingleObserver) ArrayList(java.util.ArrayList) Intent(android.content.Intent) Uri(android.net.Uri) ResolveInfo(android.content.pm.ResolveInfo) DataListPreference(com.quran.labs.androidquran.ui.preference.DataListPreference) Preference(androidx.preference.Preference) RecordingLogTree(com.quran.labs.androidquran.util.RecordingLogTree) DisposableMaybeObserver(io.reactivex.rxjava3.observers.DisposableMaybeObserver) NonNull(androidx.annotation.NonNull) File(java.io.File)

Aggregations

Activity (android.app.Activity)1 ProgressDialog (android.app.ProgressDialog)1 Context (android.content.Context)1 Intent (android.content.Intent)1 ResolveInfo (android.content.pm.ResolveInfo)1 Uri (android.net.Uri)1 AsyncTask (android.os.AsyncTask)1 Build (android.os.Build)1 Bundle (android.os.Bundle)1 Environment (android.os.Environment)1 TextUtils (android.text.TextUtils)1 Toast (android.widget.Toast)1 NonNull (androidx.annotation.NonNull)1 StringRes (androidx.annotation.StringRes)1 AlertDialog (androidx.appcompat.app.AlertDialog)1 Preference (androidx.preference.Preference)1 PreferenceFragmentCompat (androidx.preference.PreferenceFragmentCompat)1 PreferenceGroup (androidx.preference.PreferenceGroup)1 BuildConfig (com.quran.labs.androidquran.BuildConfig)1 QuranAdvancedPreferenceActivity (com.quran.labs.androidquran.QuranAdvancedPreferenceActivity)1