blob: df91c4a1f88dfc0da81fc5738b1af3344abee6a9 [file] [log] [blame]
Adam Powell23882512016-01-29 10:21:00 -08001/*
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
18package com.android.internal.app;
19
20import android.app.AlertDialog.Builder;
21import android.app.Dialog;
22import android.app.DialogFragment;
23import android.content.ComponentName;
24import android.content.DialogInterface;
25import android.content.Intent;
Alison Cichowlas1fd47152019-11-14 19:50:55 -050026import android.content.SharedPreferences;
Adam Powell23882512016-01-29 10:21:00 -080027import android.net.Uri;
28import android.os.Bundle;
29import android.provider.Settings;
30
31import com.android.internal.R;
32
33/**
34 * Shows a dialog with actions to take on a chooser target
35 */
36public class ResolverTargetActionsDialogFragment extends DialogFragment
37 implements DialogInterface.OnClickListener {
38 private static final String NAME_KEY = "componentName";
Adam Powell23882512016-01-29 10:21:00 -080039 private static final String TITLE_KEY = "title";
Alison Cichowlas1fd47152019-11-14 19:50:55 -050040 private static final String PINNED_KEY = "pinned";
Adam Powell23882512016-01-29 10:21:00 -080041
42 // Sync with R.array.resolver_target_actions_* resources
Alison Cichowlas1fd47152019-11-14 19:50:55 -050043 private static final int TOGGLE_PIN_INDEX = 0;
44 private static final int APP_INFO_INDEX = 1;
Adam Powell23882512016-01-29 10:21:00 -080045
46 public ResolverTargetActionsDialogFragment() {
47 }
48
Alison Cichowlas1fd47152019-11-14 19:50:55 -050049 public ResolverTargetActionsDialogFragment(CharSequence title, ComponentName name,
50 boolean pinned) {
Adam Powell23882512016-01-29 10:21:00 -080051 Bundle args = new Bundle();
52 args.putCharSequence(TITLE_KEY, title);
53 args.putParcelable(NAME_KEY, name);
Alison Cichowlas1fd47152019-11-14 19:50:55 -050054 args.putBoolean(PINNED_KEY, pinned);
Adam Powell23882512016-01-29 10:21:00 -080055 setArguments(args);
56 }
57
58 @Override
59 public Dialog onCreateDialog(Bundle savedInstanceState) {
60 final Bundle args = getArguments();
Alison Cichowlas1fd47152019-11-14 19:50:55 -050061 final int itemRes = args.getBoolean(PINNED_KEY, false)
62 ? R.array.resolver_target_actions_unpin
63 : R.array.resolver_target_actions_pin;
Adam Powell23882512016-01-29 10:21:00 -080064 return new Builder(getContext())
65 .setCancelable(true)
Alison Cichowlas1fd47152019-11-14 19:50:55 -050066 .setItems(itemRes, this)
Adam Powell23882512016-01-29 10:21:00 -080067 .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 Cichowlas1fd47152019-11-14 19:50:55 -050076 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 Powell23882512016-01-29 10:21:00 -080089 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}