Search in sources :

Example 1 with PreferencesActivity

use of com.amaze.filemanager.ui.activities.PreferencesActivity in project AmazeFileManager by TeamAmaze.

the class PrefFrag method onPreferenceClick.

@Override
public boolean onPreferenceClick(Preference preference) {
    final String[] sort;
    final String[] dragToMoveArray;
    MaterialDialog.Builder builder;
    MaterialDialog.Builder dragDialogBuilder;
    switch(preference.getKey()) {
        case PreferencesConstants.PREFERENCE_CLEAR_OPEN_FILE:
            OpenFileDialogFragment.Companion.clearPreferences(sharedPref);
            AppConfig.toast(getActivity(), getActivity().getString(R.string.done));
            return true;
        case PreferencesConstants.PREFERENCE_GRID_COLUMNS:
            sort = getResources().getStringArray(R.array.columns);
            builder = new MaterialDialog.Builder(getActivity());
            builder.theme(utilsProvider.getAppTheme().getMaterialDialogTheme());
            builder.title(R.string.gridcolumnno);
            int current = Integer.parseInt(sharedPref.getString(PreferencesConstants.PREFERENCE_GRID_COLUMNS, "-1"));
            current = current == -1 ? 0 : current;
            if (current != 0)
                current = current - 1;
            builder.items(sort).itemsCallbackSingleChoice(current, (dialog, view, which, text) -> {
                sharedPref.edit().putString(PreferencesConstants.PREFERENCE_GRID_COLUMNS, "" + (which != 0 ? sort[which] : "" + -1)).apply();
                dialog.dismiss();
                return true;
            });
            builder.build().show();
            return true;
        case PreferencesConstants.PREFERENCE_DRAG_AND_DROP_PREFERENCE:
            dragToMoveArray = getResources().getStringArray(R.array.dragAndDropPreference);
            dragDialogBuilder = new MaterialDialog.Builder(getActivity());
            dragDialogBuilder.theme(utilsProvider.getAppTheme().getMaterialDialogTheme());
            dragDialogBuilder.title(R.string.drag_and_drop_preference);
            int currentDragPreference = sharedPref.getInt(PreferencesConstants.PREFERENCE_DRAG_AND_DROP_PREFERENCE, PreferencesConstants.PREFERENCE_DRAG_DEFAULT);
            dragDialogBuilder.items(dragToMoveArray).itemsCallbackSingleChoice(currentDragPreference, (dialog, view, which, text) -> {
                sharedPref.edit().putInt(PreferencesConstants.PREFERENCE_DRAG_AND_DROP_PREFERENCE, which).apply();
                sharedPref.edit().putString(PreferencesConstants.PREFERENCE_DRAG_AND_DROP_REMEMBERED, null).apply();
                dialog.dismiss();
                return true;
            });
            dragDialogBuilder.build().show();
            return true;
        case PreferencesConstants.FRAGMENT_THEME:
            sort = getResources().getStringArray(R.array.theme);
            current = Integer.parseInt(sharedPref.getString(PreferencesConstants.FRAGMENT_THEME, "0"));
            builder = new MaterialDialog.Builder(getActivity());
            // builder.theme(utilsProvider.getAppTheme().getMaterialDialogTheme());
            builder.items(sort).itemsCallbackSingleChoice(current, (dialog, view, which, text) -> {
                utilsProvider.getThemeManager().setAppTheme(AppTheme.getTheme(which));
                dialog.dismiss();
                restartPC(getActivity());
                return true;
            });
            builder.title(R.string.theme);
            builder.build().show();
            return true;
        case PreferencesConstants.FRAGMENT_FEEDBACK:
            Intent emailIntent = Utils.buildEmailIntent(null, EMAIL_SUPPORT);
            PackageManager packageManager = getActivity().getPackageManager();
            List activities = packageManager.queryIntentActivities(emailIntent, PackageManager.MATCH_DEFAULT_ONLY);
            boolean isIntentSafe = activities.size() > 0;
            if (isIntentSafe)
                startActivity(Intent.createChooser(emailIntent, getResources().getString(feedback)));
            else
                Toast.makeText(getActivity(), getResources().getString(R.string.send_email_to) + " " + EMAIL_SUPPORT, Toast.LENGTH_LONG).show();
            return false;
        case PreferencesConstants.FRAGMENT_ABOUT:
            startActivity(new Intent(getActivity(), AboutActivity.class));
            return false;
        /*FROM HERE BE FRAGMENTS*/
        case PreferencesConstants.FRAGMENT_COLORS:
            ((PreferencesActivity) getActivity()).selectItem(PreferencesActivity.COLORS_PREFERENCE);
            return true;
        case PreferencesConstants.FRAGMENT_FOLDERS:
            ((PreferencesActivity) getActivity()).selectItem(PreferencesActivity.FOLDERS_PREFERENCE);
            return true;
        case PreferencesConstants.FRAGMENT_QUICKACCESSES:
            ((PreferencesActivity) getActivity()).selectItem(PreferencesActivity.QUICKACCESS_PREFERENCE);
            return true;
        case PreferencesConstants.FRAGMENT_ADVANCED_SEARCH:
            ((PreferencesActivity) getActivity()).selectItem(PreferencesActivity.ADVANCEDSEARCH_PREFERENCE);
            return true;
        case PreferencesConstants.PREFERENCE_CRYPT_MASTER_PASSWORD:
            MaterialDialog.Builder masterPasswordDialogBuilder = new MaterialDialog.Builder(getActivity());
            masterPasswordDialogBuilder.title(getResources().getString(R.string.crypt_pref_master_password_title));
            String decryptedPassword = null;
            try {
                String preferencePassword = sharedPref.getString(PreferencesConstants.PREFERENCE_CRYPT_MASTER_PASSWORD, PreferencesConstants.PREFERENCE_CRYPT_MASTER_PASSWORD_DEFAULT);
                if (!preferencePassword.equals(PreferencesConstants.PREFERENCE_CRYPT_MASTER_PASSWORD_DEFAULT)) {
                    // password is set, try to decrypt
                    decryptedPassword = CryptUtil.decryptPassword(getActivity(), preferencePassword);
                } else {
                    // no password set in preferences, just leave the field empty
                    decryptedPassword = "";
                }
            } catch (GeneralSecurityException | IOException e) {
                e.printStackTrace();
            }
            masterPasswordDialogBuilder.input(getResources().getString(R.string.authenticate_password), decryptedPassword, true, (dialog, input) -> {
            });
            masterPasswordDialogBuilder.theme(utilsProvider.getAppTheme().getMaterialDialogTheme());
            masterPasswordDialogBuilder.positiveText(getResources().getString(R.string.ok));
            masterPasswordDialogBuilder.negativeText(getResources().getString(R.string.cancel));
            masterPasswordDialogBuilder.positiveColor(((ThemedActivity) getActivity()).getAccent());
            masterPasswordDialogBuilder.negativeColor(((ThemedActivity) getActivity()).getAccent());
            masterPasswordDialogBuilder.onPositive((dialog, which) -> {
                try {
                    String inputText = dialog.getInputEditText().getText().toString();
                    if (!inputText.equals(PreferencesConstants.PREFERENCE_CRYPT_MASTER_PASSWORD_DEFAULT)) {
                        sharedPref.edit().putString(PreferencesConstants.PREFERENCE_CRYPT_MASTER_PASSWORD, CryptUtil.encryptPassword(getActivity(), dialog.getInputEditText().getText().toString())).apply();
                    } else {
                        // empty password, remove the preference
                        sharedPref.edit().putString(PreferencesConstants.PREFERENCE_CRYPT_MASTER_PASSWORD, "").apply();
                    }
                } catch (GeneralSecurityException | IOException e) {
                    e.printStackTrace();
                    sharedPref.edit().putString(PreferencesConstants.PREFERENCE_CRYPT_MASTER_PASSWORD, PreferencesConstants.PREFERENCE_CRYPT_MASTER_PASSWORD_DEFAULT).apply();
                }
            });
            masterPasswordDialogBuilder.onNegative((dialog, which) -> dialog.cancel());
            masterPasswordDialogBuilder.build().show();
            return true;
        // If there is no directory set, default to storage root (/storage/sdcard...)
        case PreferencesConstants.PREFERENCE_ZIP_EXTRACT_PATH:
            new FolderChooserDialog.Builder(getActivity()).tag(PreferencesConstants.PREFERENCE_ZIP_EXTRACT_PATH).goUpLabel(getString(R.string.folder_go_up_one_level)).chooseButton(R.string.choose_folder).cancelButton(R.string.cancel).initialPath(sharedPref.getString(PreferencesConstants.PREFERENCE_ZIP_EXTRACT_PATH, Environment.getExternalStorageDirectory().getPath())).build().show((PreferencesActivity) getActivity());
            return true;
    }
    return false;
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) PreferencesActivity(com.amaze.filemanager.ui.activities.PreferencesActivity) GeneralSecurityException(java.security.GeneralSecurityException) Intent(android.content.Intent) IOException(java.io.IOException) PackageManager(android.content.pm.PackageManager) AboutActivity(com.amaze.filemanager.ui.activities.AboutActivity) FolderChooserDialog(com.afollestad.materialdialogs.folderselector.FolderChooserDialog) List(java.util.List)

Aggregations

Intent (android.content.Intent)1 PackageManager (android.content.pm.PackageManager)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 FolderChooserDialog (com.afollestad.materialdialogs.folderselector.FolderChooserDialog)1 AboutActivity (com.amaze.filemanager.ui.activities.AboutActivity)1 PreferencesActivity (com.amaze.filemanager.ui.activities.PreferencesActivity)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 List (java.util.List)1