1 package com.koushikdutta.superuser;
3 import android.annotation.SuppressLint;
4 import android.app.Activity;
5 import android.app.AlertDialog;
6 import android.app.Dialog;
7 import android.content.Context;
8 import android.content.DialogInterface;
9 import android.content.DialogInterface.OnClickListener;
10 import android.os.Bundle;
11 import android.support.v4.app.DialogFragment;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.view.ViewGroup;
15 import android.widget.FrameLayout;
16 import android.widget.Toast;
18 import com.koushikdutta.superuser.util.Settings;
19 import com.koushikdutta.widgets.BetterListFragmentInternal;
20 import com.koushikdutta.widgets.FragmentInterfaceWrapper;
21 import com.koushikdutta.widgets.ListItem;
23 @SuppressLint("ValidFragment")
24 public class SettingsFragmentInternal extends BetterListFragmentInternal {
25 public SettingsFragmentInternal(FragmentInterfaceWrapper fragment) {
30 protected int getListFragmentResource() {
31 return R.layout.settings;
34 // static final int containerId = 100001;
35 // public static class MyPinFragment extends DialogFragment {
36 // public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
37 // FrameLayout ret = new FrameLayout(getActivity());
38 // ret.setId(containerId);
43 // public void setTitle(int title) {
44 // this.title = title;
49 // public Dialog onCreateDialog(Bundle savedInstanceState) {
50 // d = super.onCreateDialog(savedInstanceState);
52 // PinView pf = new PinView() {
54 // public void onCancel() {
60 // public void onEnter(String password) {
61 // super.onEnter(password);
62 // MyPinFragment.this.onEnter(password);
65 // getChildFragmentManager().beginTransaction().add(containerId, pf).commit();
70 // public void onEnter(String password) {
76 void confirmPin(final String pin) {
77 final Dialog d = new Dialog(getContext());
78 d.setTitle(R.string.confirm_pin);
79 d.setContentView(new PinViewHelper((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE), null, null) {
80 public void onEnter(String password) {
81 super.onEnter(password);
82 if (pin.equals(password)) {
83 Settings.setPin(getActivity(), password);
84 pinItem.setSummary(Settings.isPinProtected(getActivity()) ? R.string.pin_set : R.string.pin_protection_summary);
85 if (password != null && password.length() > 0)
86 Toast.makeText(getActivity(), getString(R.string.pin_set), Toast.LENGTH_SHORT).show();
90 Toast.makeText(getActivity(), getString(R.string.pin_mismatch), Toast.LENGTH_SHORT).show();
93 public void onCancel() {
102 final Dialog d = new Dialog(getContext());
103 d.setTitle(R.string.enter_new_pin);
104 d.setContentView(new PinViewHelper((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE), null, null) {
105 public void onEnter(String password) {
106 super.onEnter(password);
107 confirmPin(password);
111 public void onCancel() {
120 if (Settings.isPinProtected(getActivity())) {
121 final Dialog d = new Dialog(getContext());
122 d.setTitle(R.string.enter_pin);
123 d.setContentView(new PinViewHelper((LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE), null, null) {
124 public void onEnter(String password) {
125 super.onEnter(password);
126 if (Settings.checkPin(getActivity(), password)) {
127 super.onEnter(password);
132 Toast.makeText(getActivity(), getString(R.string.incorrect_pin), Toast.LENGTH_SHORT).show();
135 public void onCancel() {
148 protected void onCreate(Bundle savedInstanceState, View view) {
149 super.onCreate(savedInstanceState, view);
151 if (Settings.getMultiuserMode(getActivity()) != Settings.MULTIUSER_MODE_NONE) {
152 addItem(R.string.security, new ListItem(this, R.string.multiuser_policy, 0) {
155 switch (Settings.getMultiuserMode(getActivity())) {
156 case Settings.MULTIUSER_MODE_OWNER_MANAGED:
157 res = R.string.multiuser_owner_managed_summary;
159 case Settings.MULTIUSER_MODE_OWNER_ONLY:
160 res = R.string.multiuser_owner_only_summary;
162 case Settings.MULTIUSER_MODE_USER:
163 res = R.string.multiuser_user_summary;
167 if (!Helper.isAdminUser(getActivity())) {
171 s = getString(res) + "\n";
172 setSummary(s + getString(R.string.multiuser_require_owner));
185 public void onClick(View view) {
188 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
189 builder.setTitle(R.string.multiuser_policy);
190 String[] items = new String[] { getString(R.string.multiuser_owner_only), getString(R.string.multiuser_owner_managed), getString(R.string.multiuser_user) };
191 builder.setItems(items, new OnClickListener() {
193 public void onClick(DialogInterface dialog, int which) {
196 Settings.setMultiuserMode(getActivity(), Settings.MULTIUSER_MODE_OWNER_ONLY);
199 Settings.setMultiuserMode(getActivity(), Settings.MULTIUSER_MODE_OWNER_MANAGED);
202 Settings.setMultiuserMode(getActivity(), Settings.MULTIUSER_MODE_USER);
208 builder.create().show();
210 }).setAttrDrawable(R.attr.multiuserIcon);
213 addItem(R.string.security, new ListItem(this, R.string.declared_permission, R.string.declared_permission_summary) {
215 public void onClick(View view) {
217 Settings.setRequirePermission(getActivity(), getChecked());
220 .setAttrDrawable(R.attr.declaredPermissionsIcon)
221 .setCheckboxVisible(true)
222 .setChecked(Settings.getRequirePermission(getActivity()));
224 addItem(R.string.security, new ListItem(this, R.string.automatic_response, 0) {
226 switch (Settings.getAutomaticResponse(getActivity())) {
227 case Settings.AUTOMATIC_RESPONSE_ALLOW:
228 setSummary(R.string.automatic_response_allow_summary);
230 case Settings.AUTOMATIC_RESPONSE_DENY:
231 setSummary(R.string.automatic_response_deny_summary);
233 case Settings.AUTOMATIC_RESPONSE_PROMPT:
234 setSummary(R.string.automatic_response_prompt_summary);
243 public void onClick(View view) {
246 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
247 builder.setTitle(R.string.automatic_response);
248 String[] items = new String[] { getString(R.string.prompt), getString(R.string.deny), getString(R.string.allow) };
249 builder.setItems(items, new OnClickListener() {
251 public void onClick(DialogInterface dialog, int which) {
254 Settings.setAutomaticResponse(getActivity(), Settings.AUTOMATIC_RESPONSE_PROMPT);
257 Settings.setAutomaticResponse(getActivity(), Settings.AUTOMATIC_RESPONSE_DENY);
260 Settings.setAutomaticResponse(getActivity(), Settings.AUTOMATIC_RESPONSE_ALLOW);
266 builder.create().show();
269 .setAttrDrawable(R.attr.automaticResponseIcon);
271 pinItem = addItem(R.string.security, new ListItem(this, R.string.pin_protection, Settings.isPinProtected(getActivity()) ? R.string.pin_set : R.string.pin_protection_summary) {
273 public void onClick(View view) {
278 .setAttrDrawable(R.attr.pinProtectionIcon);
280 addItem(R.string.security, new ListItem(this, getString(R.string.request_timeout), getString(R.string.request_timeout_summary, Settings.getRequestTimeout(getActivity()))) {
282 public void onClick(View view) {
284 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
285 builder.setTitle(R.string.request_timeout);
286 String[] seconds = new String[3];
287 for (int i = 0; i < seconds.length; i++) {
288 seconds[i] = getString(R.string.number_seconds, (i + 1) * 10);
290 builder.setItems(seconds, new OnClickListener() {
292 public void onClick(DialogInterface dialog, int which) {
293 Settings.setTimeout(getActivity(), (which + 1) * 10);
294 setSummary(getString(R.string.request_timeout_summary, Settings.getRequestTimeout(getActivity())));
297 builder.create().show();
300 .setAttrDrawable(R.attr.requestTimeoutIcon);
302 addItem(R.string.settings, new ListItem(this, R.string.logging, R.string.logging_summary) {
304 public void onClick(View view) {
306 Settings.setLogging(getActivity(), getChecked());
309 .setAttrDrawable(R.attr.loggingIcon)
310 .setCheckboxVisible(true)
311 .setChecked(Settings.getLogging(getActivity()));
313 addItem(R.string.settings, new ListItem(this, R.string.notifications, 0) {
315 switch (Settings.getNotificationType(getActivity())) {
316 case Settings.NOTIFICATION_TYPE_NONE:
317 setSummary(getString(R.string.no_notification));
319 case Settings.NOTIFICATION_TYPE_NOTIFICATION:
320 setSummary(getString(R.string.notifications_summary, getString(R.string.notification)));
322 case Settings.NOTIFICATION_TYPE_TOAST:
323 setSummary(getString(R.string.notifications_summary, getString(R.string.toast)));
332 public void onClick(View view) {
335 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
336 builder.setTitle(R.string.notifications);
337 String[] items = new String[] { getString(R.string.none), getString(R.string.toast), getString(R.string.notification) };
338 builder.setItems(items, new OnClickListener() {
340 public void onClick(DialogInterface dialog, int which) {
343 Settings.setNotificationType(getActivity(), Settings.NOTIFICATION_TYPE_NONE);
346 Settings.setNotificationType(getActivity(), Settings.NOTIFICATION_TYPE_NOTIFICATION);
349 Settings.setNotificationType(getActivity(), Settings.NOTIFICATION_TYPE_TOAST);
355 builder.create().show();
358 .setAttrDrawable(R.attr.notificationsIcon);