List of usage examples for android.preference SwitchPreference setOnPreferenceClickListener
public void setOnPreferenceClickListener(OnPreferenceClickListener onPreferenceClickListener)
From source file:ota.otaupdates.Settings.java
@Override public void onCreate(Bundle savedInstanceState) { if (MainActivity.sharedPreferences.getBoolean("apptheme_light", false)) setTheme(R.style.AppTheme_Light); else/*from w ww . j av a2s .co m*/ setTheme(R.style.AppTheme_Dark); super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); SwitchPreference enable_auto_install = (SwitchPreference) findPreference("enable_auto_install"); if (!Shell.SU.available()) { enable_auto_install.setEnabled(false); enable_auto_install.setChecked(false); enable_auto_install.setSummary(getString(R.string.auto_install_root_only)); } final SwitchPreference setEnglish = (SwitchPreference) findPreference("force_english"); setEnglish.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { AlertDialog.Builder builder = new AlertDialog.Builder(Settings.this); if (MainActivity.sharedPreferences.getBoolean("force_english", false)) builder.setTitle(getString(R.string.force_english_window_title)); else builder.setTitle(getString(R.string.force_default_window_title)); builder.setMessage(getString(R.string.force_english_window_message)); builder.setPositiveButton(getString(R.string.button_yes), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { android.os.Process.killProcess(android.os.Process.myPid()); } }); builder.setNegativeButton(getString(R.string.button_no), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); AlertDialog alert = builder.create(); alert.setCancelable(true); alert.show(); return true; } }); SwitchPreference apptheme_light = (SwitchPreference) findPreference("apptheme_light"); apptheme_light.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { AlertDialog.Builder builder = new AlertDialog.Builder(Settings.this); if (MainActivity.sharedPreferences.getBoolean("apptheme_light", false)) setTheme(R.style.AppTheme_Light); else setTheme(R.style.AppTheme_Dark); builder.setMessage(getString(R.string.switch_apptheme_light_window_message)); builder.setPositiveButton(getString(R.string.button_yes), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { android.os.Process.killProcess(android.os.Process.myPid()); } }); builder.setNegativeButton(getString(R.string.button_no), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); AlertDialog alert = builder.create(); alert.setCancelable(true); alert.show(); return true; } }); findPreference("clean_junk").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { if (Build.VERSION.SDK_INT >= 23 && !checkPermission()) { // If user hasn't allowed yet, request the permission. requestPermission(); } final AlertDialog.Builder delete_dialog = new AlertDialog.Builder(Settings.this); delete_dialog.setMessage(R.string.clean_junk_dialog_summary); delete_dialog.setPositiveButton(R.string.button_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { File folder = new File(Utils.DL_PATH); File fList[] = folder.listFiles(); if (fList != null) { for (i = 0; i < fList.length; i++) { String pes = String.valueOf(fList[i]); if (pes.endsWith(".zip")) { fList[i].delete(); } } } Log.d(getString(R.string.app_name) + ": clean_junk", "Old files cleaned"); Toast.makeText(getApplicationContext(), R.string.clean_junk_toast, Toast.LENGTH_SHORT) .show(); } }); delete_dialog.setNegativeButton(R.string.button_no, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); delete_dialog.show(); return true; } }); findPreference("devs").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { clickcount++; switch (clickcount) { case 8: case 12: case 16: case 20: case 24: Toast.makeText(getApplicationContext(), R.string.crash_soon, Toast.LENGTH_SHORT).show(); break; case 28: Toast.makeText(getApplicationContext(), R.string.crash_soon_really, Toast.LENGTH_SHORT).show(); final Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { clickcount = 0; Toast.makeText(getApplicationContext(), R.string.crash_soon_gaveup, Toast.LENGTH_SHORT).show(); } }); timer.cancel(); } }, 4000, 1); break; case 35: throw new NullPointerException("Well, you pointed at something..."); } return true; } }); }
From source file:com.owncloud.android.ui.activity.Preferences.java
@SuppressWarnings("deprecation") @Override/*from w w w . j av a 2 s . c om*/ public void onCreate(Bundle savedInstanceState) { if (ThemeUtils.themingEnabled()) { setTheme(R.style.FallbackThemingTheme); } getDelegate().installViewFactory(); getDelegate().onCreate(savedInstanceState); super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); ActionBar actionBar = getDelegate().getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); ThemeUtils.setColoredTitle(actionBar, getString(R.string.actionbar_settings)); actionBar.setBackgroundDrawable(new ColorDrawable(ThemeUtils.primaryColor())); getWindow().getDecorView().setBackgroundDrawable( new ColorDrawable(ResourcesCompat.getColor(getResources(), R.color.background_color, null))); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getWindow().setStatusBarColor(ThemeUtils.primaryDarkColor()); } Drawable backArrow = getResources().getDrawable(R.drawable.ic_arrow_back); actionBar.setHomeAsUpIndicator(ThemeUtils.tintDrawable(backArrow, ThemeUtils.fontColor())); int accentColor = ThemeUtils.primaryAccentColor(); // retrieve user's base uri setupBaseUri(); // For adding content description tag to a title field in the action bar int actionBarTitleId = getResources().getIdentifier("action_bar_title", "id", "android"); View actionBarTitleView = getWindow().getDecorView().findViewById(actionBarTitleId); if (actionBarTitleView != null) { // it's null in Android 2.x getWindow().getDecorView().findViewById(actionBarTitleId) .setContentDescription(getString(R.string.actionbar_settings)); } // Load package info String temp; try { PackageInfo pkg = getPackageManager().getPackageInfo(getPackageName(), 0); temp = pkg.versionName; } catch (NameNotFoundException e) { temp = ""; Log_OC.e(TAG, "Error while showing about dialog", e); } final String appVersion = temp; // Register context menu for list of preferences. registerForContextMenu(getListView()); // General PreferenceCategory preferenceCategoryGeneral = (PreferenceCategory) findPreference("general"); preferenceCategoryGeneral .setTitle(ThemeUtils.getColoredTitle(getString(R.string.prefs_category_general), accentColor)); // Synced folders PreferenceCategory preferenceCategorySyncedFolders = (PreferenceCategory) findPreference( "synced_folders_category"); preferenceCategorySyncedFolders .setTitle(ThemeUtils.getColoredTitle(getString(R.string.drawer_synced_folders), accentColor)); PreferenceScreen preferenceScreen = (PreferenceScreen) findPreference("preference_screen"); if (!getResources().getBoolean(R.bool.syncedFolder_light)) { preferenceScreen.removePreference(preferenceCategorySyncedFolders); } else { // Upload on WiFi final ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver()); final Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()); final SwitchPreference pUploadOnWifiCheckbox = (SwitchPreference) findPreference( "synced_folder_on_wifi"); pUploadOnWifiCheckbox .setChecked(arbitraryDataProvider.getBooleanValue(account, SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI)); pUploadOnWifiCheckbox.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { arbitraryDataProvider.storeOrUpdateKeyValue(account.name, SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI, String.valueOf(pUploadOnWifiCheckbox.isChecked())); return true; } }); Preference pSyncedFolder = findPreference("synced_folders_configure_folders"); if (pSyncedFolder != null) { if (getResources().getBoolean(R.bool.syncedFolder_light) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { pSyncedFolder.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { Intent syncedFoldersIntent = new Intent(getApplicationContext(), SyncedFoldersActivity.class); syncedFoldersIntent.putExtra(SyncedFoldersActivity.EXTRA_SHOW_SIDEBAR, false); startActivity(syncedFoldersIntent); return true; } }); } else { preferenceCategorySyncedFolders.removePreference(pSyncedFolder); } } } PreferenceCategory preferenceCategoryDetails = (PreferenceCategory) findPreference("details"); preferenceCategoryDetails .setTitle(ThemeUtils.getColoredTitle(getString(R.string.prefs_category_details), accentColor)); boolean fPassCodeEnabled = getResources().getBoolean(R.bool.passcode_enabled); pCode = (SwitchPreference) findPreference(PassCodeActivity.PREFERENCE_SET_PASSCODE); if (pCode != null && fPassCodeEnabled) { pCode.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { Intent i = new Intent(getApplicationContext(), PassCodeActivity.class); Boolean incoming = (Boolean) newValue; i.setAction(incoming ? PassCodeActivity.ACTION_REQUEST_WITH_RESULT : PassCodeActivity.ACTION_CHECK_WITH_RESULT); startActivityForResult(i, incoming ? ACTION_REQUEST_PASSCODE : ACTION_CONFIRM_PASSCODE); // Don't update just yet, we will decide on it in onActivityResult return false; } }); } else { preferenceCategoryDetails.removePreference(pCode); } boolean fPrintEnabled = getResources().getBoolean(R.bool.fingerprint_enabled); fPrint = (SwitchPreference) findPreference(PREFERENCE_USE_FINGERPRINT); if (fPrint != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (FingerprintActivity.isFingerprintCapable(MainApp.getAppContext()) && fPrintEnabled) { fPrint.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { Boolean incoming = (Boolean) newValue; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (FingerprintActivity.isFingerprintReady(MainApp.getAppContext())) { SharedPreferences appPrefs = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = appPrefs.edit(); editor.putBoolean("use_fingerprint", incoming); editor.apply(); return true; } else { if (incoming) { Toast.makeText(MainApp.getAppContext(), R.string.prefs_fingerprint_notsetup, Toast.LENGTH_LONG).show(); fPrint.setChecked(false); } SharedPreferences appPrefs = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = appPrefs.edit(); editor.putBoolean("use_fingerprint", false); editor.apply(); return false; } } else { return false; } } }); if (!FingerprintActivity.isFingerprintReady(MainApp.getAppContext())) { fPrint.setChecked(false); } } else { preferenceCategoryDetails.removePreference(fPrint); } } else { preferenceCategoryDetails.removePreference(fPrint); } } boolean fShowHiddenFilesEnabled = getResources().getBoolean(R.bool.show_hidden_files_enabled); mShowHiddenFiles = (SwitchPreference) findPreference("show_hidden_files"); if (fShowHiddenFilesEnabled) { mShowHiddenFiles.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { SharedPreferences appPrefs = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = appPrefs.edit(); editor.putBoolean("show_hidden_files_pref", mShowHiddenFiles.isChecked()); editor.apply(); return true; } }); } else { preferenceCategoryDetails.removePreference(mShowHiddenFiles); } mExpertMode = (SwitchPreference) findPreference(EXPERT_MODE); if (getResources().getBoolean(R.bool.syncedFolder_light)) { preferenceCategoryDetails.removePreference(mExpertMode); } else { mExpertMode = (SwitchPreference) findPreference(EXPERT_MODE); mExpertMode.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { SharedPreferences appPrefs = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = appPrefs.edit(); editor.putBoolean(EXPERT_MODE, mExpertMode.isChecked()); editor.apply(); if (mExpertMode.isChecked()) { Log_OC.startLogging(getApplicationContext()); } else { if (!BuildConfig.DEBUG && !getApplicationContext().getResources().getBoolean(R.bool.logger_enabled)) { Log_OC.stopLogging(); } } return true; } }); } PreferenceCategory preferenceCategoryMore = (PreferenceCategory) findPreference("more"); preferenceCategoryMore .setTitle(ThemeUtils.getColoredTitle(getString(R.string.prefs_category_more), accentColor)); boolean calendarContactsEnabled = getResources().getBoolean(R.bool.davdroid_integration_enabled); Preference pCalendarContacts = findPreference("calendar_contacts"); if (pCalendarContacts != null) { if (calendarContactsEnabled) { pCalendarContacts.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { try { launchDavDroidLogin(); } catch (Throwable t) { Log_OC.e(TAG, "Base Uri for account could not be resolved to call DAVdroid!", t); Toast.makeText(MainApp.getAppContext(), R.string.prefs_calendar_contacts_address_resolve_error, Toast.LENGTH_SHORT) .show(); } return true; } }); } else { preferenceCategoryMore.removePreference(pCalendarContacts); } } boolean contactsBackupEnabled = !getResources().getBoolean(R.bool.show_drawer_contacts_backup) && getResources().getBoolean(R.bool.contacts_backup); Preference pContactsBackup = findPreference("contacts"); if (pCalendarContacts != null) { if (contactsBackupEnabled) { pContactsBackup.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { Intent contactsIntent = new Intent(getApplicationContext(), ContactsPreferenceActivity.class); contactsIntent.putExtra(ContactsPreferenceActivity.EXTRA_SHOW_SIDEBAR, false); startActivity(contactsIntent); return true; } }); } else { preferenceCategoryMore.removePreference(pContactsBackup); } } if (!fShowHiddenFilesEnabled && !fPrintEnabled && !fPassCodeEnabled) { preferenceScreen.removePreference(preferenceCategoryDetails); } boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled); Preference pHelp = findPreference("help"); if (pHelp != null) { if (helpEnabled) { pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { String helpWeb = getString(R.string.url_help); if (helpWeb != null && helpWeb.length() > 0) { Uri uriUrl = Uri.parse(helpWeb); Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl); startActivity(intent); } return true; } }); } else { preferenceCategoryMore.removePreference(pHelp); } } boolean recommendEnabled = getResources().getBoolean(R.bool.recommend_enabled); Preference pRecommend = findPreference("recommend"); if (pRecommend != null) { if (recommendEnabled) { pRecommend.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); String appName = getString(R.string.app_name); String downloadUrlGooglePlayStore = getString(R.string.url_app_download); String downloadUrlFDroid = getString(R.string.fdroid_link); String downloadUrls = String.format(getString(R.string.recommend_urls), downloadUrlGooglePlayStore, downloadUrlFDroid); String recommendSubject = String.format(getString(R.string.recommend_subject), appName); String recommendText = String.format(getString(R.string.recommend_text), appName, downloadUrls); intent.putExtra(Intent.EXTRA_SUBJECT, recommendSubject); intent.putExtra(Intent.EXTRA_TEXT, recommendText); startActivity(intent); return true; } }); } else { preferenceCategoryMore.removePreference(pRecommend); } } boolean feedbackEnabled = getResources().getBoolean(R.bool.feedback_enabled); Preference pFeedback = findPreference("feedback"); if (pFeedback != null) { if (feedbackEnabled) { pFeedback.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { String feedbackMail = getString(R.string.mail_feedback); String feedback = getText(R.string.prefs_feedback) + " - android v" + appVersion; Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_SUBJECT, feedback); intent.setData(Uri.parse(feedbackMail)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); return true; } }); } else { preferenceCategoryMore.removePreference(pFeedback); } } SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); boolean loggerEnabled = getResources().getBoolean(R.bool.logger_enabled) || BuildConfig.DEBUG || appPrefs.getBoolean(EXPERT_MODE, false); Preference pLogger = findPreference("logger"); if (pLogger != null) { if (loggerEnabled) { pLogger.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { Intent loggerIntent = new Intent(getApplicationContext(), LogHistoryActivity.class); startActivity(loggerIntent); return true; } }); } else { preferenceCategoryMore.removePreference(pLogger); } } boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled); Preference pImprint = findPreference("imprint"); if (pImprint != null) { if (imprintEnabled) { pImprint.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { String imprintWeb = getString(R.string.url_imprint); if (imprintWeb != null && imprintWeb.length() > 0) { Uri uriUrl = Uri.parse(imprintWeb); Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl); startActivity(intent); } //ImprintDialog.newInstance(true).show(preference.get, "IMPRINT_DIALOG"); return true; } }); } else { preferenceCategoryMore.removePreference(pImprint); } } mPrefStoragePath = (ListPreference) findPreference(PreferenceKeys.STORAGE_PATH); if (mPrefStoragePath != null) { StoragePoint[] storageOptions = DataStorageProvider.getInstance().getAvailableStoragePoints(); String[] entries = new String[storageOptions.length]; String[] values = new String[storageOptions.length]; for (int i = 0; i < storageOptions.length; ++i) { entries[i] = storageOptions[i].getDescription(); values[i] = storageOptions[i].getPath(); } mPrefStoragePath.setEntries(entries); mPrefStoragePath.setEntryValues(values); mPrefStoragePath.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { String newPath = (String) newValue; if (mStoragePath.equals(newPath)) { return true; } StorageMigration storageMigration = new StorageMigration(Preferences.this, mStoragePath, newPath); storageMigration.setStorageMigrationProgressListener(Preferences.this); storageMigration.migrate(); return false; } }); } // About category PreferenceCategory preferenceCategoryAbout = (PreferenceCategory) findPreference("about"); preferenceCategoryAbout .setTitle(ThemeUtils.getColoredTitle(getString(R.string.prefs_category_about), accentColor)); /* About App */ pAboutApp = findPreference("about_app"); if (pAboutApp != null) { pAboutApp.setTitle(String.format(getString(R.string.about_android), getString(R.string.app_name))); pAboutApp.setSummary(String.format(getString(R.string.about_version), appVersion)); } // source code boolean sourcecodeEnabled = getResources().getBoolean(R.bool.sourcecode_enabled); Preference sourcecodePreference = findPreference("sourcecode"); if (sourcecodePreference != null) { String sourcecodeUrl = getString(R.string.sourcecode_url); if (sourcecodeEnabled && !sourcecodeUrl.isEmpty()) { sourcecodePreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { Uri uriUrl = Uri.parse(sourcecodeUrl); Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl); startActivity(intent); return true; } }); } else { preferenceCategoryAbout.removePreference(sourcecodePreference); } } // license boolean licenseEnabled = getResources().getBoolean(R.bool.license_enabled); Preference licensePreference = findPreference("license"); if (licensePreference != null) { String licenseUrl = getString(R.string.license_url); if (licenseEnabled && !licenseUrl.isEmpty()) { licensePreference.setSummary(R.string.prefs_gpl_v2); licensePreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { Uri uriUrl = Uri.parse(licenseUrl); Intent intent = new Intent(Intent.ACTION_VIEW, uriUrl); startActivity(intent); return true; } }); } else { preferenceCategoryAbout.removePreference(licensePreference); } } // privacy boolean privacyEnabled = getResources().getBoolean(R.bool.privacy_enabled); Preference privacyPreference = findPreference("privacy"); if (privacyPreference != null) { if (privacyEnabled) { privacyPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { String privacyUrl = getString(R.string.privacy_url); if (privacyUrl.length() > 0) { Intent externalWebViewIntent = new Intent(getApplicationContext(), ExternalSiteWebView.class); externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE, getResources().getString(R.string.privacy)); externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, privacyUrl); externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, false); externalWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_MENU_ITEM_ID, -1); startActivity(externalWebViewIntent); } return true; } }); } else { preferenceCategoryAbout.removePreference(privacyPreference); } } loadExternalSettingLinks(preferenceCategoryMore); loadStoragePath(); }