|
| 1 | +/* |
| 2 | + * Copyright (C) 2014, ParanoidAndroid Project |
| 3 | + * Copyrighy (c) 2015, METALLIUM OS Project |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package android.util; |
| 19 | + |
| 20 | +import android.app.ActivityManager; |
| 21 | +import android.app.AlertDialog; |
| 22 | +import android.content.Context; |
| 23 | +import android.content.DialogInterface; |
| 24 | +import android.graphics.drawable.Drawable; |
| 25 | +import android.view.LayoutInflater; |
| 26 | +import android.view.View; |
| 27 | +import android.view.Window; |
| 28 | +import android.view.WindowManager; |
| 29 | +import android.widget.GifView; |
| 30 | +import android.widget.ImageView; |
| 31 | +import android.provider.Settings; |
| 32 | + |
| 33 | +import java.io.InputStream; |
| 34 | + |
| 35 | +import com.android.internal.R; |
| 36 | + |
| 37 | +/** |
| 38 | + * Hide from public API |
| 39 | + * @hide |
| 40 | + */ |
| 41 | +public class SettingConfirmationHelper { |
| 42 | + |
| 43 | + private static final int NOT_SET = 0; |
| 44 | + private static final int ENABLED = 1; |
| 45 | + private static final int DISABLED = 2; |
| 46 | + private static final int ASK_LATER = 3; |
| 47 | + |
| 48 | + /** |
| 49 | + * @hide |
| 50 | + */ |
| 51 | + public static interface OnSelectListener { |
| 52 | + void onSelect(boolean enabled); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @hide |
| 57 | + */ |
| 58 | + public static void showConfirmationDialogForSetting(final Context context, String title, String msg, Drawable hint, |
| 59 | + final String setting, final OnSelectListener listener) { |
| 60 | + int mCurrentStatus = Settings.System.getInt(context.getContentResolver(), setting, NOT_SET); |
| 61 | + if (mCurrentStatus == ENABLED || mCurrentStatus == DISABLED) return; |
| 62 | + |
| 63 | + LayoutInflater layoutInflater = LayoutInflater.from(context); |
| 64 | + View dialogLayout = layoutInflater.inflate(R.layout.setting_confirmation_dialog, null); |
| 65 | + final ImageView visualHint = (ImageView) |
| 66 | + dialogLayout.findViewById(R.id.setting_confirmation_dialog_visual_hint); |
| 67 | + visualHint.setImageDrawable(hint); |
| 68 | + visualHint.setVisibility(View.VISIBLE); |
| 69 | + |
| 70 | + AlertDialog dialog = createDialog(context,title,msg,dialogLayout,setting,listener); |
| 71 | + initWindow(dialog).show(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @hide |
| 76 | + */ |
| 77 | + public static void showConfirmationDialogForSetting(final Context context, String title, String msg, InputStream gif, |
| 78 | + final String setting, final OnSelectListener listener) { |
| 79 | + int mCurrentStatus = Settings.System.getInt( |
| 80 | + context.getContentResolver(), setting, NOT_SET); |
| 81 | + if (mCurrentStatus == ENABLED || mCurrentStatus == DISABLED) return; |
| 82 | + |
| 83 | + LayoutInflater layoutInflater = LayoutInflater.from(context); |
| 84 | + View dialogLayout = layoutInflater.inflate(R.layout.setting_confirmation_dialog, null); |
| 85 | + final GifView gifView = (GifView) |
| 86 | + dialogLayout.findViewById(R.id.setting_confirmation_dialog_visual_gif); |
| 87 | + gifView.setGifResource(gif); |
| 88 | + gifView.setVisibility(View.VISIBLE); |
| 89 | + |
| 90 | + AlertDialog dialog = createDialog(context,title,msg,dialogLayout,setting,listener); |
| 91 | + initWindow(dialog).show(); |
| 92 | + } |
| 93 | + |
| 94 | + private static AlertDialog initWindow(AlertDialog dialog) { |
| 95 | + Window dialogWindow = dialog.getWindow(); |
| 96 | + dialogWindow.setType(WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL); |
| 97 | + return dialog; |
| 98 | + } |
| 99 | + |
| 100 | + private static AlertDialog createDialog(final Context context, String title, String msg, View view, |
| 101 | + final String setting, final OnSelectListener listener) { |
| 102 | + final int currentUserId = ActivityManager.getCurrentUser(); |
| 103 | + AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 104 | + |
| 105 | + builder.setView(view, 10, 10, 10, 20); |
| 106 | + builder.setTitle(title); |
| 107 | + builder.setMessage(msg); |
| 108 | + builder.setPositiveButton(R.string.setting_confirmation_yes, |
| 109 | + new DialogInterface.OnClickListener() { |
| 110 | + public void onClick(DialogInterface dialog, int which) { |
| 111 | + Settings.System.putIntForUser(context.getContentResolver(), setting, |
| 112 | + ENABLED, currentUserId); |
| 113 | + if (listener == null) return; |
| 114 | + listener.onSelect(true); |
| 115 | + } |
| 116 | + } |
| 117 | + ); |
| 118 | + builder.setNeutralButton(R.string.setting_confirmation_ask_me_later, |
| 119 | + new DialogInterface.OnClickListener() { |
| 120 | + public void onClick(DialogInterface dialog, int which) { |
| 121 | + Settings.System.putIntForUser(context.getContentResolver(), setting, |
| 122 | + ASK_LATER, currentUserId); |
| 123 | + if (listener == null) return; |
| 124 | + listener.onSelect(false); |
| 125 | + } |
| 126 | + } |
| 127 | + ); |
| 128 | + builder.setNegativeButton(R.string.setting_confirmation_no, |
| 129 | + new DialogInterface.OnClickListener() { |
| 130 | + public void onClick(DialogInterface dialog, int which) { |
| 131 | + Settings.System.putIntForUser(context.getContentResolver(), setting, |
| 132 | + DISABLED, currentUserId); |
| 133 | + if (listener == null) return; |
| 134 | + listener.onSelect(false); |
| 135 | + } |
| 136 | + } |
| 137 | + ); |
| 138 | + builder.setCancelable(false); |
| 139 | + |
| 140 | + return builder.create(); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments