Search in sources :

Example 1 with PreferencesActivity

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

the class PrefFrag method onPreferenceClick.

@Override
public boolean onPreferenceClick(Preference preference) {
    final String[] sort;
    MaterialDialog.Builder builder;
    switch(preference.getKey()) {
        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, new MaterialDialog.ListCallbackSingleChoice() {

                @Override
                public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                    sharedPref.edit().putString(PreferencesConstants.PREFERENCE_GRID_COLUMNS, "" + (which != 0 ? sort[which] : "" + -1)).commit();
                    dialog.dismiss();
                    return true;
                }
            });
            builder.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, new MaterialDialog.ListCallbackSingleChoice() {

                @Override
                public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence 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 = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "vishalmeham2@gmail.com", null));
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Feedback : Amaze File Manager");
            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) + " vishalmeham2@gmail.com", 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, false, new MaterialDialog.InputCallback() {

                @Override
                public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
                }
            });
            masterPasswordDialogBuilder.theme(utilsProvider.getAppTheme().getMaterialDialogTheme());
            masterPasswordDialogBuilder.positiveText(getResources().getString(R.string.ok));
            masterPasswordDialogBuilder.negativeText(getResources().getString(R.string.cancel));
            masterPasswordDialogBuilder.positiveColor(utilsProvider.getColorPreference().getColor(ColorUsage.ACCENT));
            masterPasswordDialogBuilder.negativeColor(utilsProvider.getColorPreference().getColor(ColorUsage.ACCENT));
            masterPasswordDialogBuilder.onPositive(new MaterialDialog.SingleButtonCallback() {

                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction 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(new MaterialDialog.SingleButtonCallback() {

                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    dialog.cancel();
                }
            });
            masterPasswordDialogBuilder.build().show();
            return true;
    }
    return false;
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) PreferencesActivity(com.amaze.filemanager.activities.PreferencesActivity) GeneralSecurityException(java.security.GeneralSecurityException) Intent(android.content.Intent) IOException(java.io.IOException) View(android.view.View) ListView(android.widget.ListView) PackageManager(android.content.pm.PackageManager) AboutActivity(com.amaze.filemanager.activities.AboutActivity) DialogAction(com.afollestad.materialdialogs.DialogAction) List(java.util.List)

Aggregations

Intent (android.content.Intent)1 PackageManager (android.content.pm.PackageManager)1 View (android.view.View)1 ListView (android.widget.ListView)1 DialogAction (com.afollestad.materialdialogs.DialogAction)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 AboutActivity (com.amaze.filemanager.activities.AboutActivity)1 PreferencesActivity (com.amaze.filemanager.activities.PreferencesActivity)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 List (java.util.List)1