Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | package com.android.internal.app; |
| 19 | |
| 20 | import android.app.AlertDialog.Builder; |
| 21 | import android.app.Dialog; |
| 22 | import android.app.DialogFragment; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.DialogInterface; |
| 25 | import android.content.Intent; |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 26 | import android.content.SharedPreferences; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 27 | import android.net.Uri; |
| 28 | import android.os.Bundle; |
| 29 | import android.provider.Settings; |
| 30 | |
| 31 | import com.android.internal.R; |
| 32 | |
| 33 | /** |
| 34 | * Shows a dialog with actions to take on a chooser target |
| 35 | */ |
| 36 | public class ResolverTargetActionsDialogFragment extends DialogFragment |
| 37 | implements DialogInterface.OnClickListener { |
| 38 | private static final String NAME_KEY = "componentName"; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 39 | private static final String TITLE_KEY = "title"; |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 40 | private static final String PINNED_KEY = "pinned"; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 41 | |
| 42 | // Sync with R.array.resolver_target_actions_* resources |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 43 | private static final int TOGGLE_PIN_INDEX = 0; |
| 44 | private static final int APP_INFO_INDEX = 1; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 45 | |
| 46 | public ResolverTargetActionsDialogFragment() { |
| 47 | } |
| 48 | |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 49 | public ResolverTargetActionsDialogFragment(CharSequence title, ComponentName name, |
| 50 | boolean pinned) { |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 51 | Bundle args = new Bundle(); |
| 52 | args.putCharSequence(TITLE_KEY, title); |
| 53 | args.putParcelable(NAME_KEY, name); |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 54 | args.putBoolean(PINNED_KEY, pinned); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 55 | setArguments(args); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 60 | final Bundle args = getArguments(); |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 61 | final int itemRes = args.getBoolean(PINNED_KEY, false) |
| 62 | ? R.array.resolver_target_actions_unpin |
| 63 | : R.array.resolver_target_actions_pin; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 64 | return new Builder(getContext()) |
| 65 | .setCancelable(true) |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 66 | .setItems(itemRes, this) |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 67 | .setTitle(args.getCharSequence(TITLE_KEY)) |
| 68 | .create(); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void onClick(DialogInterface dialog, int which) { |
| 73 | final Bundle args = getArguments(); |
| 74 | ComponentName name = args.getParcelable(NAME_KEY); |
| 75 | switch (which) { |
Alison Cichowlas | 1fd4715 | 2019-11-14 19:50:55 -0500 | [diff] [blame] | 76 | case TOGGLE_PIN_INDEX: |
| 77 | SharedPreferences sp = ChooserActivity.getPinnedSharedPrefs(getContext()); |
| 78 | final String key = name.flattenToString(); |
| 79 | boolean currentVal = sp.getBoolean(name.flattenToString(), false); |
| 80 | if (currentVal) { |
| 81 | sp.edit().remove(key).apply(); |
| 82 | } else { |
| 83 | sp.edit().putBoolean(key, true).apply(); |
| 84 | } |
| 85 | |
| 86 | // Force the chooser to requery and resort things |
| 87 | getActivity().recreate(); |
| 88 | break; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 89 | case APP_INFO_INDEX: |
| 90 | Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) |
| 91 | .setData(Uri.fromParts("package", name.getPackageName(), null)) |
| 92 | .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
| 93 | startActivity(in); |
| 94 | break; |
| 95 | } |
| 96 | dismiss(); |
| 97 | } |
| 98 | } |