blob: 32543c82621e58ee50166bf13364bcade0823614 [file] [log] [blame]
Jeff Sharkey21de56a2014-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 McKayb83976072016-02-18 11:32:26 -080019import static com.android.documentsui.services.FileOperationService.OPERATION_DELETE;
Steve McKaya6bbeab2016-02-17 15:02:01 -080020import static com.android.documentsui.services.FileOperationService.OPERATION_MOVE;
Steve McKayb83976072016-02-18 11:32:26 -080021import static com.android.documentsui.services.FileOperationService.OPERATION_UNKNOWN;
Steve McKaya6bbeab2016-02-17 15:02:01 -080022import static com.android.internal.util.Preconditions.checkArgument;
23
Daichi Hironode021ab2015-04-14 12:16:59 +090024import android.app.Activity;
Jeff Sharkey21de56a2014-04-05 19:05:24 -070025import android.app.Fragment;
26import android.app.FragmentManager;
27import android.app.FragmentTransaction;
28import android.os.Bundle;
Jeff Sharkey21de56a2014-04-05 19:05:24 -070029import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.Button;
33
34import com.android.documentsui.model.DocumentInfo;
Steve McKaya6bbeab2016-02-17 15:02:01 -080035import com.android.documentsui.services.FileOperationService.OpType;
Jeff Sharkey21de56a2014-04-05 19:05:24 -070036
Jeff Sharkey21de56a2014-04-05 19:05:24 -070037/**
38 * Display pick confirmation bar, usually for selecting a directory.
39 */
40public class PickFragment extends Fragment {
41 public static final String TAG = "PickFragment";
42
Daichi Hironoac990ce2015-05-27 15:20:10 -070043 private int mAction;
Steve McKayb83976072016-02-18 11:32:26 -080044 // Only legal values are OPERATION_COPY, OPERATION_MOVE, and unset (OPERATION_UNKNOWN).
45 private @OpType int mCopyOperationSubType = OPERATION_UNKNOWN;
Jeff Sharkey21de56a2014-04-05 19:05:24 -070046 private DocumentInfo mPickTarget;
Jeff Sharkey21de56a2014-04-05 19:05:24 -070047 private View mContainer;
48 private Button mPick;
Daichi Hironode021ab2015-04-14 12:16:59 +090049 private Button mCancel;
Jeff Sharkey21de56a2014-04-05 19:05:24 -070050
51 public static void show(FragmentManager fm) {
Daichi Hironoac990ce2015-05-27 15:20:10 -070052 // Fragment can be restored by FragmentManager automatically.
53 if (get(fm) != null) {
54 return;
55 }
Jeff Sharkey21de56a2014-04-05 19:05:24 -070056
Daichi Hironoac990ce2015-05-27 15:20:10 -070057 final PickFragment fragment = new PickFragment();
Jeff Sharkey21de56a2014-04-05 19:05:24 -070058 final FragmentTransaction ft = fm.beginTransaction();
59 ft.replace(R.id.container_save, fragment, TAG);
60 ft.commitAllowingStateLoss();
61 }
62
63 public static PickFragment get(FragmentManager fm) {
64 return (PickFragment) fm.findFragmentByTag(TAG);
65 }
66
67 @Override
68 public View onCreateView(
69 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
70 mContainer = inflater.inflate(R.layout.fragment_pick, container, false);
71
72 mPick = (Button) mContainer.findViewById(android.R.id.button1);
73 mPick.setOnClickListener(mPickListener);
74
Daichi Hironode021ab2015-04-14 12:16:59 +090075 mCancel = (Button) mContainer.findViewById(android.R.id.button2);
76 mCancel.setOnClickListener(mCancelListener);
77
Daichi Hironoac990ce2015-05-27 15:20:10 -070078 updateView();
Jeff Sharkey21de56a2014-04-05 19:05:24 -070079 return mContainer;
80 }
81
82 private View.OnClickListener mPickListener = new View.OnClickListener() {
83 @Override
84 public void onClick(View v) {
Steve McKayef3e2cf2015-04-20 17:18:15 -070085 final DocumentsActivity activity = DocumentsActivity.get(PickFragment.this);
Jeff Sharkey21de56a2014-04-05 19:05:24 -070086 activity.onPickRequested(mPickTarget);
87 }
88 };
89
Daichi Hironode021ab2015-04-14 12:16:59 +090090 private View.OnClickListener mCancelListener = new View.OnClickListener() {
91 @Override
92 public void onClick(View v) {
93 final BaseActivity activity = BaseActivity.get(PickFragment.this);
94 activity.setResult(Activity.RESULT_CANCELED);
95 activity.finish();
96 }
97 };
Jeff Sharkey21de56a2014-04-05 19:05:24 -070098
Daichi Hironode021ab2015-04-14 12:16:59 +090099 /**
Steve McKay7a3b88c2015-09-23 17:21:40 -0700100 * @param action Which action defined in State is the picker shown for.
Daichi Hironode021ab2015-04-14 12:16:59 +0900101 */
Steve McKayb83976072016-02-18 11:32:26 -0800102 public void setPickTarget(
103 int action, @OpType int copyOperationSubType, DocumentInfo pickTarget) {
104 checkArgument(copyOperationSubType != OPERATION_DELETE);
Daichi Hironoac990ce2015-05-27 15:20:10 -0700105 mAction = action;
Steve McKayb83976072016-02-18 11:32:26 -0800106 mCopyOperationSubType = copyOperationSubType;
Daichi Hironode021ab2015-04-14 12:16:59 +0900107 mPickTarget = pickTarget;
Daichi Hironoac990ce2015-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 McKay7a3b88c2015-09-23 17:21:40 -0700118 case State.ACTION_OPEN_TREE:
Daichi Hironoac990ce2015-05-27 15:20:10 -0700119 mPick.setText(R.string.button_select);
120 mCancel.setVisibility(View.GONE);
121 break;
Ben Kwa84cebbe2015-09-25 14:48:29 -0700122 case State.ACTION_PICK_COPY_DESTINATION:
Steve McKayb83976072016-02-18 11:32:26 -0800123 mPick.setText(mCopyOperationSubType == OPERATION_MOVE
Steve McKaya6bbeab2016-02-17 15:02:01 -0800124 ? R.string.button_move : R.string.button_copy);
Daichi Hironoac990ce2015-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 McKay7a3b88c2015-09-23 17:21:40 -0700133 mAction == State.ACTION_OPEN_TREE ||
Daichi Hironoac990ce2015-05-27 15:20:10 -0700134 mPickTarget.isCreateSupported())) {
135 mContainer.setVisibility(View.VISIBLE);
136 } else {
137 mContainer.setVisibility(View.GONE);
138 }
Jeff Sharkey21de56a2014-04-05 19:05:24 -0700139 }
140}