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 19 import static com.android.documentsui.services.FileOperationService.OPERATION_DELETE; 20 import static com.android.documentsui.services.FileOperationService.OPERATION_MOVE; 21 import static com.android.documentsui.services.FileOperationService.OPERATION_UNKNOWN; 22 23 import android.app.Activity; 24 import android.app.Fragment; 25 import android.app.FragmentManager; 26 import android.app.FragmentTransaction; 27 import android.os.Bundle; 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; 34 import com.android.documentsui.services.FileOperationService.OpType; 35 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 42 private int mAction; 43 // Only legal values are OPERATION_COPY, OPERATION_MOVE, and unset (OPERATION_UNKNOWN). 44 private @OpType int mCopyOperationSubType = OPERATION_UNKNOWN; 45 private DocumentInfo mPickTarget; 46 private View mContainer; 47 private Button mPick; 48 private Button mCancel; 49 50 public static void show(FragmentManager fm) { 51 // Fragment can be restored by FragmentManager automatically. 52 if (get(fm) != null) { 53 return; 54 } 55 56 final PickFragment fragment = new PickFragment(); 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 74 mCancel = (Button) mContainer.findViewById(android.R.id.button2); 75 mCancel.setOnClickListener(mCancelListener); 76 77 updateView(); 78 return mContainer; 79 } 80 81 private View.OnClickListener mPickListener = new View.OnClickListener() { 82 @Override 83 public void onClick(View v) { 84 final DocumentsActivity activity = DocumentsActivity.get(PickFragment.this); 85 activity.onPickRequested(mPickTarget); 86 } 87 }; 88 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 }; 97 98 /** 99 * @param action Which action defined in State is the picker shown for. 100 */ 101 public void setPickTarget( 102 int action, @OpType int copyOperationSubType, DocumentInfo pickTarget) { 103 assert(copyOperationSubType != OPERATION_DELETE); 104 105 mAction = action; 106 mCopyOperationSubType = copyOperationSubType; 107 mPickTarget = pickTarget; 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) { 118 case State.ACTION_OPEN_TREE: 119 mPick.setText(R.string.button_select); 120 mCancel.setVisibility(View.GONE); 121 break; 122 case State.ACTION_PICK_COPY_DESTINATION: 123 mPick.setText(mCopyOperationSubType == OPERATION_MOVE 124 ? R.string.button_move : R.string.button_copy); 125 mCancel.setVisibility(View.VISIBLE); 126 break; 127 default: 128 mContainer.setVisibility(View.GONE); 129 return; 130 } 131 132 if (mPickTarget != null && ( 133 mAction == State.ACTION_OPEN_TREE || 134 mPickTarget.isCreateSupported())) { 135 mContainer.setVisibility(View.VISIBLE); 136 } else { 137 mContainer.setVisibility(View.GONE); 138 } 139 } 140 } 141