Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [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 | package com.android.documentsui.picker; |
| 18 | |
| 19 | import android.content.Intent; |
| 20 | import android.content.pm.ResolveInfo; |
| 21 | import android.net.Uri; |
| 22 | import android.provider.Settings; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 23 | import android.util.Log; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 24 | |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 25 | import com.android.documentsui.AbstractActionHandler; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 26 | import com.android.documentsui.Metrics; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 27 | import com.android.documentsui.base.DocumentInfo; |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 28 | import com.android.documentsui.base.DocumentStack; |
| 29 | import com.android.documentsui.base.RootInfo; |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 30 | import com.android.documentsui.dirlist.DocumentDetails; |
| 31 | import com.android.documentsui.dirlist.FragmentTuner; |
| 32 | import com.android.documentsui.dirlist.Model; |
| 33 | import com.android.documentsui.dirlist.MultiSelectManager; |
| 34 | |
| 35 | import javax.annotation.Nullable; |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Provides {@link PickActivity} action specializations to fragments. |
| 39 | */ |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 40 | class ActionHandler extends AbstractActionHandler<PickActivity> { |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 41 | |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 42 | private static final String TAG = "PickerActionHandler"; |
| 43 | |
| 44 | private final FragmentTuner mTuner; |
| 45 | private final Config mConfig; |
| 46 | |
| 47 | ActionHandler(PickActivity activity, FragmentTuner tuner) { |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 48 | super(activity); |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 49 | mTuner = tuner; |
| 50 | mConfig = new Config(); |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void showAppDetails(ResolveInfo info) { |
| 55 | final Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); |
| 56 | intent.setData(Uri.fromParts("package", info.activityInfo.packageName, null)); |
| 57 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
| 58 | mActivity.startActivity(intent); |
| 59 | } |
| 60 | |
| 61 | @Override |
Steve McKay | 739f94b | 2016-09-22 14:54:23 -0700 | [diff] [blame] | 62 | public void openInNewWindow(DocumentStack path) { |
| 63 | // Open new window support only depends on vanilla Activity, so it is |
| 64 | // implemented in our parent class. But we don't support that in |
| 65 | // picking. So as a matter of defensiveness, we override that here. |
| 66 | throw new UnsupportedOperationException("Can't open in new window"); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void openRoot(RootInfo root) { |
| 71 | Metrics.logRootVisited(mActivity, root); |
| 72 | mActivity.onRootPicked(root); |
| 73 | } |
| 74 | |
| 75 | @Override |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 76 | public void openRoot(ResolveInfo info) { |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 77 | Metrics.logAppVisited(mActivity, info); |
| 78 | mActivity.onAppPicked(info); |
| 79 | } |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 80 | |
Steve McKay | 5f29694 | 2016-09-23 08:40:07 -0700 | [diff] [blame^] | 81 | |
| 82 | @Override |
| 83 | public boolean viewDocument(DocumentDetails details) { |
| 84 | return openDocument(details); |
| 85 | } |
| 86 | |
Steve McKay | 6d20d19 | 2016-09-21 17:57:10 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public boolean openDocument(DocumentDetails details) { |
| 89 | DocumentInfo doc = mConfig.model.getDocument(details.getModelId()); |
| 90 | if (doc == null) { |
| 91 | Log.w(TAG, |
| 92 | "Can't view item. No Document available for modeId: " + details.getModelId()); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | if (mTuner.isDocumentEnabled(doc.mimeType, doc.flags)) { |
| 97 | mActivity.onDocumentPicked(doc, mConfig.model); |
| 98 | mConfig.selectionMgr.clearSelection(); |
| 99 | return true; |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | ActionHandler reset(Model model, MultiSelectManager selectionMgr) { |
| 105 | mConfig.reset(model, selectionMgr); |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | private static final class Config { |
| 110 | |
| 111 | @Nullable Model model; |
| 112 | @Nullable MultiSelectManager selectionMgr; |
| 113 | |
| 114 | public void reset(Model model, MultiSelectManager selectionMgr) { |
| 115 | assert(model != null); |
| 116 | assert(selectionMgr != null); |
| 117 | |
| 118 | this.model = model; |
| 119 | this.selectionMgr = selectionMgr; |
| 120 | } |
| 121 | } |
Garfield Tan | 7d75f7b | 2016-09-20 16:33:24 -0700 | [diff] [blame] | 122 | } |