Back to project page detectivemode.
The source code is released under:
GNU General Public License
If you think the Android project detectivemode listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package net.dotslashzero.detectivemode; /*from w w w . j a va 2s.co m*/ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import android.annotation.SuppressLint; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.util.Log; import android.view.ViewGroup; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Switch; /** * Responsible for making the app/module interact with the persistent * configuration file. */ public class ConfigCheck { private static final String[] BROWSER_LIST = { "com.android.browser", "com.android.chrome", "org.mozilla.firefox" }; private static Object syncObject = new Object(); @SuppressLint("WorldReadableFiles") @SuppressWarnings("deprecation") public static void initPreferences(Context context) { SharedPreferences sharedPrefs = context.getSharedPreferences("settings", Context.MODE_WORLD_READABLE); for (String browserPackageName : ConfigCheck.BROWSER_LIST) { if (!sharedPrefs.contains(browserPackageName)) { synchronized (syncObject) { sharedPrefs.edit().putBoolean(browserPackageName, false).commit(); } } } } @SuppressLint("WorldReadableFiles") @SuppressWarnings("deprecation") private static boolean getPreference(Context context, String preferenceName) { SharedPreferences sharedPrefs = context.getSharedPreferences("settings", Context.MODE_WORLD_READABLE); return (sharedPrefs.getBoolean(preferenceName, false)); } @SuppressLint("WorldReadableFiles") @SuppressWarnings("deprecation") private static void setPreference(Context context, String preferenceName, boolean value) { SharedPreferences sharedPrefs = context.getSharedPreferences("settings", Context.MODE_WORLD_READABLE); synchronized (syncObject) { sharedPrefs.edit().putBoolean(preferenceName, value).commit(); } return; } private static void onOptionToggled(CompoundButton buttonView, boolean isChecked) { String tag = (String) buttonView.getTag(); Log.i(buttonView.getContext().getString(R.string.app_name), "CheckedChange event for \"" + tag + "\", " + "new value: " + (isChecked ? "checked" : "unchecked")); ConfigCheck.setPreference(buttonView.getContext(), tag, isChecked); return; } public static int populateList(LinearLayout browserList) { int result = 0; browserList.removeAllViews(); final Context context = browserList.getContext(); final PackageManager packageMgr = context.getPackageManager(); final List<ApplicationInfo> installedApps = packageMgr.getInstalledApplications(PackageManager.GET_META_DATA); final List<String> supportedBrowsers = Arrays.asList(ConfigCheck.BROWSER_LIST); List<ApplicationInfo> installedBrowserApps = new ArrayList<ApplicationInfo>(); for (ApplicationInfo app : installedApps) { if (supportedBrowsers.contains((String) app.packageName)) { installedBrowserApps.add(app); } } Collections.sort(installedBrowserApps, new Comparator<ApplicationInfo>() { public int compare(ApplicationInfo a, ApplicationInfo b) { return (a.packageName.compareTo(b.packageName)); } }); for (ApplicationInfo app : installedBrowserApps) { LinearLayout rowLayout = new LinearLayout(context); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); layoutParams.topMargin = 25; rowLayout.setLayoutParams(layoutParams); rowLayout.setOrientation(LinearLayout.HORIZONTAL); ImageView appIcon = new ImageView(context); appIcon.setImageDrawable(app.loadIcon(packageMgr)); appIcon.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)); appIcon.setEnabled(app.enabled); rowLayout.addView(appIcon); Switch appConfigToggle = new Switch(context); appConfigToggle.setTag(app.packageName); appConfigToggle.setText(packageMgr.getApplicationLabel(app) + System.lineSeparator() + app.packageName); appConfigToggle.setChecked(ConfigCheck.getPreference(context, app.packageName)); appConfigToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { ConfigCheck.onOptionToggled(buttonView, isChecked); return; } }); appConfigToggle.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); appConfigToggle.setEnabled(app.enabled); rowLayout.addView(appConfigToggle); browserList.addView(rowLayout); } return (result); } private ConfigCheck() { } }