• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 package com.android.documentsui.picker;
18 
19 import android.app.Dialog;
20 import android.content.DialogInterface;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.PackageManager;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.provider.DocumentsContract;
26 import android.text.TextUtils;
27 
28 import androidx.fragment.app.DialogFragment;
29 import androidx.fragment.app.Fragment;
30 import androidx.fragment.app.FragmentManager;
31 import androidx.fragment.app.FragmentTransaction;
32 
33 import com.android.documentsui.BaseActivity;
34 import com.android.documentsui.R;
35 import com.android.documentsui.base.DocumentInfo;
36 import com.android.documentsui.base.Shared;
37 
38 import com.google.android.material.dialog.MaterialAlertDialogBuilder;
39 
40 /**
41  * Used to confirm with user that it's OK to overwrite an existing file.
42  */
43 public class ConfirmFragment extends DialogFragment {
44 
45     private static final String TAG = "ConfirmFragment";
46 
47     public static final String CONFIRM_TYPE = "type";
48     public static final int TYPE_OVERWRITE = 1;
49     public static final int TYPE_OEPN_TREE = 2;
50 
51     private ActionHandler<PickActivity> mActions;
52     private DocumentInfo mTarget;
53     private int mType;
54 
55     @Override
onActivityCreated(Bundle savedInstanceState)56     public void onActivityCreated(Bundle savedInstanceState) {
57         super.onActivityCreated(savedInstanceState);
58 
59         mActions = ((PickActivity) getActivity()).getInjector().actions;
60     }
61 
62     @Override
onCreateDialog(Bundle savedInstanceState)63     public Dialog onCreateDialog(Bundle savedInstanceState) {
64         Bundle arg = (getArguments() != null) ? getArguments() : savedInstanceState;
65 
66         mTarget = arg.getParcelable(Shared.EXTRA_DOC);
67         mType = arg.getInt(CONFIRM_TYPE);
68         final PickResult pickResult = ((PickActivity) getActivity()).getInjector().pickResult;
69 
70         final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getActivity());
71         switch (mType) {
72             case TYPE_OVERWRITE:
73                 String message = String.format(
74                         getString(R.string.overwrite_file_confirmation_message),
75                         mTarget.displayName);
76                 builder.setMessage(message);
77                 builder.setPositiveButton(
78                         android.R.string.ok,
79                         (DialogInterface dialog, int id) -> {
80                             pickResult.increaseActionCount();
81                             mActions.finishPicking(mTarget.derivedUri);
82                         });
83                 break;
84             case TYPE_OEPN_TREE:
85                 final Uri uri = DocumentsContract.buildTreeDocumentUri(
86                         mTarget.authority, mTarget.documentId);
87                 final BaseActivity activity = (BaseActivity) getActivity();
88                 final String target = activity.getCurrentTitle();
89                 final String location = activity.getCurrentRoot().title;
90                 final String text = getString(R.string.open_tree_dialog_title, target, location);
91                 message = getString(R.string.open_tree_dialog_message,
92                         getAppName(getActivity().getCallingPackage()));
93 
94                 builder.setTitle(text);
95                 builder.setMessage(message);
96                 builder.setPositiveButton(
97                         R.string.allow,
98                         (DialogInterface dialog, int id) -> {
99                             pickResult.increaseActionCount();
100                             mActions.finishPicking(uri);
101                         });
102                 break;
103 
104         }
105         builder.setNegativeButton(android.R.string.cancel,
106                 (DialogInterface dialog, int id) -> pickResult.increaseActionCount());
107 
108         return builder.create();
109     }
110 
111     @Override
onSaveInstanceState(Bundle outState)112     public void onSaveInstanceState(Bundle outState) {
113         super.onSaveInstanceState(outState);
114 
115         outState.putParcelable(Shared.EXTRA_DOC, mTarget);
116         outState.putInt(CONFIRM_TYPE, mType);
117     }
118 
getAppName(String packageName)119     private String getAppName(String packageName) {
120         final String anonymous = getString(R.string.anonymous_application);
121         if (TextUtils.isEmpty(packageName)) {
122             return anonymous;
123         }
124 
125         final PackageManager pm = getContext().getPackageManager();
126         ApplicationInfo ai;
127         try {
128             ai = pm.getApplicationInfo(packageName, 0);
129         } catch (final PackageManager.NameNotFoundException e) {
130             return anonymous;
131         }
132 
133         CharSequence result = pm.getApplicationLabel(ai);
134         return TextUtils.isEmpty(result) ? anonymous : result.toString();
135     }
136 
show(FragmentManager fm, DocumentInfo overwriteTarget, int type)137     public static void show(FragmentManager fm, DocumentInfo overwriteTarget, int type) {
138         Bundle arg = new Bundle();
139         arg.putParcelable(Shared.EXTRA_DOC, overwriteTarget);
140         arg.putInt(CONFIRM_TYPE, type);
141 
142         FragmentTransaction ft = fm.beginTransaction();
143         Fragment f = new ConfirmFragment();
144         f.setArguments(arg);
145         ft.add(f, TAG);
146         ft.commitAllowingStateLoss();
147     }
148 }
149