blob: 933506c258f2a57d08093fc9e97ba320d2a1327f [file] [log] [blame]
Jeff Sharkey6e565ff2014-04-05 19:05:24 -07001/*
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
17package com.android.documentsui;
18
Steve McKayc8bdc7f2016-02-18 11:32:26 -080019import static com.android.documentsui.services.FileOperationService.OPERATION_DELETE;
Steve McKayf1719342016-02-17 15:02:01 -080020import static com.android.documentsui.services.FileOperationService.OPERATION_MOVE;
Steve McKayc8bdc7f2016-02-18 11:32:26 -080021import static com.android.documentsui.services.FileOperationService.OPERATION_UNKNOWN;
Steve McKayf1719342016-02-17 15:02:01 -080022
Daichi Hirono676453c2015-04-14 12:16:59 +090023import android.app.Activity;
Jeff Sharkey6e565ff2014-04-05 19:05:24 -070024import android.app.Fragment;
25import android.app.FragmentManager;
26import android.app.FragmentTransaction;
27import android.os.Bundle;
Jeff Sharkey6e565ff2014-04-05 19:05:24 -070028import android.view.LayoutInflater;
29import android.view.View;
30import android.view.ViewGroup;
31import android.widget.Button;
32
33import com.android.documentsui.model.DocumentInfo;
Steve McKayf1719342016-02-17 15:02:01 -080034import com.android.documentsui.services.FileOperationService.OpType;
Jeff Sharkey6e565ff2014-04-05 19:05:24 -070035
Jeff Sharkey6e565ff2014-04-05 19:05:24 -070036/**
37 * Display pick confirmation bar, usually for selecting a directory.
38 */
39public class PickFragment extends Fragment {
40 public static final String TAG = "PickFragment";
41
Daichi Hirono4e2bb182015-05-27 15:20:10 -070042 private int mAction;
Steve McKayc8bdc7f2016-02-18 11:32:26 -080043 // Only legal values are OPERATION_COPY, OPERATION_MOVE, and unset (OPERATION_UNKNOWN).
44 private @OpType int mCopyOperationSubType = OPERATION_UNKNOWN;
Jeff Sharkey6e565ff2014-04-05 19:05:24 -070045 private DocumentInfo mPickTarget;
Jeff Sharkey6e565ff2014-04-05 19:05:24 -070046 private View mContainer;
47 private Button mPick;
Daichi Hirono676453c2015-04-14 12:16:59 +090048 private Button mCancel;
Jeff Sharkey6e565ff2014-04-05 19:05:24 -070049
50 public static void show(FragmentManager fm) {
Daichi Hirono4e2bb182015-05-27 15:20:10 -070051 // Fragment can be restored by FragmentManager automatically.
52 if (get(fm) != null) {
53 return;
54 }
Jeff Sharkey6e565ff2014-04-05 19:05:24 -070055
Daichi Hirono4e2bb182015-05-27 15:20:10 -070056 final PickFragment fragment = new PickFragment();
Jeff Sharkey6e565ff2014-04-05 19:05:24 -070057 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 Hirono676453c2015-04-14 12:16:59 +090074 mCancel = (Button) mContainer.findViewById(android.R.id.button2);
75 mCancel.setOnClickListener(mCancelListener);
76
Daichi Hirono4e2bb182015-05-27 15:20:10 -070077 updateView();
Jeff Sharkey6e565ff2014-04-05 19:05:24 -070078 return mContainer;
79 }
80
81 private View.OnClickListener mPickListener = new View.OnClickListener() {
82 @Override
83 public void onClick(View v) {
Steve McKayb68dd222015-04-20 17:18:15 -070084 final DocumentsActivity activity = DocumentsActivity.get(PickFragment.this);
Jeff Sharkey6e565ff2014-04-05 19:05:24 -070085 activity.onPickRequested(mPickTarget);
86 }
87 };
88
Daichi Hirono676453c2015-04-14 12:16:59 +090089 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 Sharkey6e565ff2014-04-05 19:05:24 -070097
Daichi Hirono676453c2015-04-14 12:16:59 +090098 /**
Steve McKayf8a5e082015-09-23 17:21:40 -070099 * @param action Which action defined in State is the picker shown for.
Daichi Hirono676453c2015-04-14 12:16:59 +0900100 */
Steve McKayc8bdc7f2016-02-18 11:32:26 -0800101 public void setPickTarget(
102 int action, @OpType int copyOperationSubType, DocumentInfo pickTarget) {
Steve McKay0af8afd2016-02-25 13:34:03 -0800103 assert(copyOperationSubType != OPERATION_DELETE);
104
Daichi Hirono4e2bb182015-05-27 15:20:10 -0700105 mAction = action;
Steve McKayc8bdc7f2016-02-18 11:32:26 -0800106 mCopyOperationSubType = copyOperationSubType;
Daichi Hirono676453c2015-04-14 12:16:59 +0900107 mPickTarget = pickTarget;
Daichi Hirono4e2bb182015-05-27 15:20:10 -0700108 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 McKayf8a5e082015-09-23 17:21:40 -0700118 case State.ACTION_OPEN_TREE:
Daichi Hirono4e2bb182015-05-27 15:20:10 -0700119 mPick.setText(R.string.button_select);
120 mCancel.setVisibility(View.GONE);
121 break;
Ben Kwaae967802015-09-25 14:48:29 -0700122 case State.ACTION_PICK_COPY_DESTINATION:
Steve McKayc8bdc7f2016-02-18 11:32:26 -0800123 mPick.setText(mCopyOperationSubType == OPERATION_MOVE
Steve McKayf1719342016-02-17 15:02:01 -0800124 ? R.string.button_move : R.string.button_copy);
Daichi Hirono4e2bb182015-05-27 15:20:10 -0700125 mCancel.setVisibility(View.VISIBLE);
126 break;
127 default:
128 mContainer.setVisibility(View.GONE);
129 return;
130 }
131
132 if (mPickTarget != null && (
Steve McKayf8a5e082015-09-23 17:21:40 -0700133 mAction == State.ACTION_OPEN_TREE ||
Daichi Hirono4e2bb182015-05-27 15:20:10 -0700134 mPickTarget.isCreateSupported())) {
135 mContainer.setVisibility(View.VISIBLE);
136 } else {
137 mContainer.setVisibility(View.GONE);
138 }
Jeff Sharkey6e565ff2014-04-05 19:05:24 -0700139 }
140}