blob: 21efc78bddf80819fe691a723f61fcf0cf5fccec [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;
Alison Cichowlas19ee2922019-12-16 19:43:12 -050027import android.content.res.Configuration;
Adam Powell23882512016-01-29 10:21:00 -080028import android.net.Uri;
29import android.os.Bundle;
30import android.provider.Settings;
31
32import com.android.internal.R;
Alison Cichowlas19ee2922019-12-16 19:43:12 -050033import com.android.internal.app.chooser.DisplayResolveInfo;
34
35import java.util.ArrayList;
36import java.util.List;
Adam Powell23882512016-01-29 10:21:00 -080037
38/**
Alison Cichowlas19ee2922019-12-16 19:43:12 -050039 * Shows a dialog with actions to take on a chooser target.
Adam Powell23882512016-01-29 10:21:00 -080040 */
41public class ResolverTargetActionsDialogFragment extends DialogFragment
42 implements DialogInterface.OnClickListener {
43 private static final String NAME_KEY = "componentName";
Adam Powell23882512016-01-29 10:21:00 -080044 private static final String TITLE_KEY = "title";
Alison Cichowlas1fd47152019-11-14 19:50:55 -050045 private static final String PINNED_KEY = "pinned";
Adam Powell23882512016-01-29 10:21:00 -080046
47 // Sync with R.array.resolver_target_actions_* resources
Alison Cichowlas1fd47152019-11-14 19:50:55 -050048 private static final int TOGGLE_PIN_INDEX = 0;
49 private static final int APP_INFO_INDEX = 1;
Adam Powell23882512016-01-29 10:21:00 -080050
Alison Cichowlas19ee2922019-12-16 19:43:12 -050051 private List<DisplayResolveInfo> mTargetInfos = new ArrayList<>();
52 private List<CharSequence> mLabels = new ArrayList<>();
53 private boolean[] mPinned;
54
Adam Powell23882512016-01-29 10:21:00 -080055 public ResolverTargetActionsDialogFragment() {
56 }
57
Alison Cichowlas1fd47152019-11-14 19:50:55 -050058 public ResolverTargetActionsDialogFragment(CharSequence title, ComponentName name,
59 boolean pinned) {
Adam Powell23882512016-01-29 10:21:00 -080060 Bundle args = new Bundle();
61 args.putCharSequence(TITLE_KEY, title);
62 args.putParcelable(NAME_KEY, name);
Alison Cichowlas1fd47152019-11-14 19:50:55 -050063 args.putBoolean(PINNED_KEY, pinned);
Adam Powell23882512016-01-29 10:21:00 -080064 setArguments(args);
65 }
66
Alison Cichowlas19ee2922019-12-16 19:43:12 -050067 public ResolverTargetActionsDialogFragment(CharSequence title, ComponentName name,
68 List<DisplayResolveInfo> targets, List<CharSequence> labels) {
69 Bundle args = new Bundle();
70 args.putCharSequence(TITLE_KEY, title);
71 args.putParcelable(NAME_KEY, name);
72 mTargetInfos = targets;
73 mLabels = labels;
74 setArguments(args);
75 }
76
Adam Powell23882512016-01-29 10:21:00 -080077 @Override
78 public Dialog onCreateDialog(Bundle savedInstanceState) {
79 final Bundle args = getArguments();
Alison Cichowlas1fd47152019-11-14 19:50:55 -050080 final int itemRes = args.getBoolean(PINNED_KEY, false)
81 ? R.array.resolver_target_actions_unpin
82 : R.array.resolver_target_actions_pin;
Alison Cichowlas19ee2922019-12-16 19:43:12 -050083 String[] defaultActions = getResources().getStringArray(itemRes);
84 CharSequence[] items;
85
86 if (mTargetInfos == null || mTargetInfos.size() < 2) {
87 items = defaultActions;
88 } else {
89 // Pin item for each sub-item
90 items = new CharSequence[mTargetInfos.size() + 1];
91 for (int i = 0; i < mTargetInfos.size(); i++) {
92 items[i] = mTargetInfos.get(i).isPinned()
93 ? getResources().getString(R.string.unpin_specific_target, mLabels.get(i))
94 : getResources().getString(R.string.pin_specific_target, mLabels.get(i));
95 }
96 // "App info"
97 items[mTargetInfos.size()] = defaultActions[1];
98 }
99
100
Adam Powell23882512016-01-29 10:21:00 -0800101 return new Builder(getContext())
102 .setCancelable(true)
Alison Cichowlas19ee2922019-12-16 19:43:12 -0500103 .setItems(items, this)
Adam Powell23882512016-01-29 10:21:00 -0800104 .setTitle(args.getCharSequence(TITLE_KEY))
105 .create();
106 }
107
108 @Override
109 public void onClick(DialogInterface dialog, int which) {
110 final Bundle args = getArguments();
111 ComponentName name = args.getParcelable(NAME_KEY);
Alison Cichowlas19ee2922019-12-16 19:43:12 -0500112 if (which == 0 || (mTargetInfos.size() > 0 && which < mTargetInfos.size())) {
113 if (mTargetInfos == null || mTargetInfos.size() == 0) {
114 pinComponent(name);
115 } else {
116 pinComponent(mTargetInfos.get(which).getResolvedComponentName());
117 }
118 // Force the chooser to requery and resort things
Alison Cichowlasbc290812019-12-17 19:42:29 -0500119 ((ChooserActivity) getActivity()).handlePackagesChanged();
Alison Cichowlas19ee2922019-12-16 19:43:12 -0500120 } else {
121 // Last item in dialog is App Info
122 Intent in = new Intent().setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
123 .setData(Uri.fromParts("package", name.getPackageName(), null))
124 .addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
125 startActivity(in);
Adam Powell23882512016-01-29 10:21:00 -0800126 }
127 dismiss();
128 }
Alison Cichowlas19ee2922019-12-16 19:43:12 -0500129
130 private void pinComponent(ComponentName name) {
131 SharedPreferences sp = ChooserActivity.getPinnedSharedPrefs(getContext());
132 final String key = name.flattenToString();
133 boolean currentVal = sp.getBoolean(name.flattenToString(), false);
134 if (currentVal) {
135 sp.edit().remove(key).apply();
136 } else {
137 sp.edit().putBoolean(key, true).apply();
138 }
139 }
140
141 @Override
142 public void onConfigurationChanged(Configuration newConfig) {
143 // Dismiss on config changed (eg: rotation)
144 // TODO: Maintain state on config change
145 super.onConfigurationChanged(newConfig);
146 dismiss();
147 }
148
Adam Powell23882512016-01-29 10:21:00 -0800149}