Search in sources :

Example 1 with YalpStorePermissionManager

use of com.github.yeriomin.yalpstore.YalpStorePermissionManager in project YalpStore by yeriomin.

the class DownloadDirectory method draw.

@Override
public void draw() {
    preference.setSummary(Paths.getYalpPath(activity).getAbsolutePath());
    preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            YalpStorePermissionManager permissionManager = new YalpStorePermissionManager(activity);
            if (!permissionManager.checkPermission()) {
                permissionManager.requestPermission();
            }
            return true;
        }
    });
    preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object o) {
            String newValue = (String) o;
            boolean result = checkNewValue(newValue);
            if (!result) {
                if (ContextUtil.isAlive(activity) && !((EditTextPreference) preference).getText().equals(Paths.FALLBACK_DIRECTORY)) {
                    getFallbackDialog().show();
                } else {
                    ContextUtil.toast(activity, R.string.error_downloads_directory_not_writable);
                }
            } else {
                try {
                    preference.setSummary(new File(Paths.getStorageRoot(activity), newValue).getCanonicalPath());
                } catch (IOException e) {
                    Log.i(getClass().getName(), "checkNewValue returned true, but drawing the path \"" + newValue + "\" in the summary failed... strange");
                    return false;
                }
            }
            return result;
        }

        private boolean checkNewValue(String newValue) {
            try {
                File storageRoot = Paths.getStorageRoot(activity);
                File newDir = new File(storageRoot, newValue).getCanonicalFile();
                if (!newDir.getCanonicalPath().startsWith(storageRoot.getCanonicalPath())) {
                    return false;
                }
                if (newDir.exists()) {
                    return newDir.canWrite();
                }
                if (activity.checkCallingOrSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                    return newDir.mkdirs();
                }
                return true;
            } catch (IOException e) {
                return false;
            }
        }

        private DialogWrapperAbstract getFallbackDialog() {
            return new DialogWrapper(activity).setMessage(activity.getString(R.string.error_downloads_directory_not_writable) + "\n\n" + activity.getString(R.string.pref_message_fallback, Paths.FALLBACK_DIRECTORY)).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    preference.setText(Paths.FALLBACK_DIRECTORY);
                    preference.getOnPreferenceChangeListener().onPreferenceChange(preference, Paths.FALLBACK_DIRECTORY);
                    dialog.dismiss();
                }
            }).create();
        }
    });
}
Also used : DialogWrapperAbstract(com.github.yeriomin.yalpstore.view.DialogWrapperAbstract) DialogInterface(android.content.DialogInterface) EditTextPreference(android.preference.EditTextPreference) IOException(java.io.IOException) DialogWrapper(com.github.yeriomin.yalpstore.view.DialogWrapper) EditTextPreference(android.preference.EditTextPreference) Preference(android.preference.Preference) File(java.io.File) YalpStorePermissionManager(com.github.yeriomin.yalpstore.YalpStorePermissionManager)

Aggregations

DialogInterface (android.content.DialogInterface)1 EditTextPreference (android.preference.EditTextPreference)1 Preference (android.preference.Preference)1 YalpStorePermissionManager (com.github.yeriomin.yalpstore.YalpStorePermissionManager)1 DialogWrapper (com.github.yeriomin.yalpstore.view.DialogWrapper)1 DialogWrapperAbstract (com.github.yeriomin.yalpstore.view.DialogWrapperAbstract)1 File (java.io.File)1 IOException (java.io.IOException)1