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
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;
26import android.content.SharedPreferences;
27import 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";
39    private static final String PINNED_KEY = "pinned";
40    private static final String TITLE_KEY = "title";
41
42    // Sync with R.array.resolver_target_actions_* resources
43    private static final int TOGGLE_PIN_INDEX = 0;
44    private static final int APP_INFO_INDEX = 1;
45
46    public ResolverTargetActionsDialogFragment() {
47    }
48
49    public ResolverTargetActionsDialogFragment(CharSequence title, ComponentName name,
50            boolean pinned) {
51        Bundle args = new Bundle();
52        args.putCharSequence(TITLE_KEY, title);
53        args.putParcelable(NAME_KEY, name);
54        args.putBoolean(PINNED_KEY, pinned);
55        setArguments(args);
56    }
57
58    @Override
59    public Dialog onCreateDialog(Bundle savedInstanceState) {
60        final Bundle args = getArguments();
61        final int itemRes = args.getBoolean(PINNED_KEY, false)
62                ? R.array.resolver_target_actions_unpin
63                : R.array.resolver_target_actions_pin;
64        return new Builder(getContext())
65                .setCancelable(true)
66                .setItems(itemRes, this)
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) {
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;
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}
99