Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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; |
| 18 | |
Steve McKay | c8bdc7f | 2016-02-18 11:32:26 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.services.FileOperationService.OPERATION_DELETE; |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 20 | import static com.android.documentsui.services.FileOperationService.OPERATION_MOVE; |
Steve McKay | c8bdc7f | 2016-02-18 11:32:26 -0800 | [diff] [blame] | 21 | import static com.android.documentsui.services.FileOperationService.OPERATION_UNKNOWN; |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 22 | |
Daichi Hirono | 676453c | 2015-04-14 12:16:59 +0900 | [diff] [blame] | 23 | import android.app.Activity; |
Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 24 | import android.app.Fragment; |
| 25 | import android.app.FragmentManager; |
| 26 | import android.app.FragmentTransaction; |
| 27 | import android.os.Bundle; |
Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 28 | import android.view.LayoutInflater; |
| 29 | import android.view.View; |
| 30 | import android.view.ViewGroup; |
| 31 | import android.widget.Button; |
| 32 | |
| 33 | import com.android.documentsui.model.DocumentInfo; |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 34 | import com.android.documentsui.services.FileOperationService.OpType; |
Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 35 | |
Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Display pick confirmation bar, usually for selecting a directory. |
| 38 | */ |
| 39 | public class PickFragment extends Fragment { |
| 40 | public static final String TAG = "PickFragment"; |
| 41 | |
Daichi Hirono | 4e2bb18 | 2015-05-27 15:20:10 -0700 | [diff] [blame] | 42 | private int mAction; |
Steve McKay | c8bdc7f | 2016-02-18 11:32:26 -0800 | [diff] [blame] | 43 | // Only legal values are OPERATION_COPY, OPERATION_MOVE, and unset (OPERATION_UNKNOWN). |
| 44 | private @OpType int mCopyOperationSubType = OPERATION_UNKNOWN; |
Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 45 | private DocumentInfo mPickTarget; |
Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 46 | private View mContainer; |
| 47 | private Button mPick; |
Daichi Hirono | 676453c | 2015-04-14 12:16:59 +0900 | [diff] [blame] | 48 | private Button mCancel; |
Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 49 | |
| 50 | public static void show(FragmentManager fm) { |
Daichi Hirono | 4e2bb18 | 2015-05-27 15:20:10 -0700 | [diff] [blame] | 51 | // Fragment can be restored by FragmentManager automatically. |
| 52 | if (get(fm) != null) { |
| 53 | return; |
| 54 | } |
Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 55 | |
Daichi Hirono | 4e2bb18 | 2015-05-27 15:20:10 -0700 | [diff] [blame] | 56 | final PickFragment fragment = new PickFragment(); |
Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 57 | final FragmentTransaction ft = fm.beginTransaction(); |
| 58 | ft.replace(R.id.container_save, fragment, TAG); |
| 59 | ft.commitAllowingStateLoss(); |
| 60 | } |
| 61 | |
| 62 | public static PickFragment get(FragmentManager fm) { |
| 63 | return (PickFragment) fm.findFragmentByTag(TAG); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public View onCreateView( |
| 68 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 69 | mContainer = inflater.inflate(R.layout.fragment_pick, container, false); |
| 70 | |
| 71 | mPick = (Button) mContainer.findViewById(android.R.id.button1); |
| 72 | mPick.setOnClickListener(mPickListener); |
| 73 | |
Daichi Hirono | 676453c | 2015-04-14 12:16:59 +0900 | [diff] [blame] | 74 | mCancel = (Button) mContainer.findViewById(android.R.id.button2); |
| 75 | mCancel.setOnClickListener(mCancelListener); |
| 76 | |
Daichi Hirono | 4e2bb18 | 2015-05-27 15:20:10 -0700 | [diff] [blame] | 77 | updateView(); |
Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 78 | return mContainer; |
| 79 | } |
| 80 | |
| 81 | private View.OnClickListener mPickListener = new View.OnClickListener() { |
| 82 | @Override |
| 83 | public void onClick(View v) { |
Steve McKay | b68dd22 | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 84 | final DocumentsActivity activity = DocumentsActivity.get(PickFragment.this); |
Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 85 | activity.onPickRequested(mPickTarget); |
| 86 | } |
| 87 | }; |
| 88 | |
Daichi Hirono | 676453c | 2015-04-14 12:16:59 +0900 | [diff] [blame] | 89 | private View.OnClickListener mCancelListener = new View.OnClickListener() { |
| 90 | @Override |
| 91 | public void onClick(View v) { |
| 92 | final BaseActivity activity = BaseActivity.get(PickFragment.this); |
| 93 | activity.setResult(Activity.RESULT_CANCELED); |
| 94 | activity.finish(); |
| 95 | } |
| 96 | }; |
Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 97 | |
Daichi Hirono | 676453c | 2015-04-14 12:16:59 +0900 | [diff] [blame] | 98 | /** |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 99 | * @param action Which action defined in State is the picker shown for. |
Daichi Hirono | 676453c | 2015-04-14 12:16:59 +0900 | [diff] [blame] | 100 | */ |
Steve McKay | c8bdc7f | 2016-02-18 11:32:26 -0800 | [diff] [blame] | 101 | public void setPickTarget( |
| 102 | int action, @OpType int copyOperationSubType, DocumentInfo pickTarget) { |
Steve McKay | 0af8afd | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 103 | assert(copyOperationSubType != OPERATION_DELETE); |
| 104 | |
Daichi Hirono | 4e2bb18 | 2015-05-27 15:20:10 -0700 | [diff] [blame] | 105 | mAction = action; |
Steve McKay | c8bdc7f | 2016-02-18 11:32:26 -0800 | [diff] [blame] | 106 | mCopyOperationSubType = copyOperationSubType; |
Daichi Hirono | 676453c | 2015-04-14 12:16:59 +0900 | [diff] [blame] | 107 | mPickTarget = pickTarget; |
Daichi Hirono | 4e2bb18 | 2015-05-27 15:20:10 -0700 | [diff] [blame] | 108 | if (mContainer != null) { |
| 109 | updateView(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Applies the state of fragment to the view components. |
| 115 | */ |
| 116 | private void updateView() { |
| 117 | switch (mAction) { |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 118 | case State.ACTION_OPEN_TREE: |
Daichi Hirono | 4e2bb18 | 2015-05-27 15:20:10 -0700 | [diff] [blame] | 119 | mPick.setText(R.string.button_select); |
| 120 | mCancel.setVisibility(View.GONE); |
| 121 | break; |
Ben Kwa | ae96780 | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 122 | case State.ACTION_PICK_COPY_DESTINATION: |
Steve McKay | c8bdc7f | 2016-02-18 11:32:26 -0800 | [diff] [blame] | 123 | mPick.setText(mCopyOperationSubType == OPERATION_MOVE |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 124 | ? R.string.button_move : R.string.button_copy); |
Daichi Hirono | 4e2bb18 | 2015-05-27 15:20:10 -0700 | [diff] [blame] | 125 | mCancel.setVisibility(View.VISIBLE); |
| 126 | break; |
| 127 | default: |
| 128 | mContainer.setVisibility(View.GONE); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | if (mPickTarget != null && ( |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 133 | mAction == State.ACTION_OPEN_TREE || |
Daichi Hirono | 4e2bb18 | 2015-05-27 15:20:10 -0700 | [diff] [blame] | 134 | mPickTarget.isCreateSupported())) { |
| 135 | mContainer.setVisibility(View.VISIBLE); |
| 136 | } else { |
| 137 | mContainer.setVisibility(View.GONE); |
| 138 | } |
Jeff Sharkey | 6e565ff | 2014-04-05 19:05:24 -0700 | [diff] [blame] | 139 | } |
| 140 | } |