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 17package com.android.documentsui; 18 19import android.R.string; 20import android.app.Activity; 21import android.app.Fragment; 22import android.app.FragmentManager; 23import android.app.FragmentTransaction; 24import android.os.Bundle; 25import android.text.TextUtils; 26import android.view.LayoutInflater; 27import android.view.View; 28import android.view.ViewGroup; 29import android.widget.Button; 30 31import com.android.documentsui.model.DocumentInfo; 32 33import java.util.Locale; 34 35/** 36 * Display pick confirmation bar, usually for selecting a directory. 37 */ 38public class PickFragment extends Fragment { 39 public static final String TAG = "PickFragment"; 40 41 private int mAction; 42 private DocumentInfo mPickTarget; 43 private View mContainer; 44 private Button mPick; 45 private Button mCancel; 46 47 public static void show(FragmentManager fm) { 48 // Fragment can be restored by FragmentManager automatically. 49 if (get(fm) != null) { 50 return; 51 } 52 53 final PickFragment fragment = new PickFragment(); 54 final FragmentTransaction ft = fm.beginTransaction(); 55 ft.replace(R.id.container_save, fragment, TAG); 56 ft.commitAllowingStateLoss(); 57 } 58 59 public static PickFragment get(FragmentManager fm) { 60 return (PickFragment) fm.findFragmentByTag(TAG); 61 } 62 63 @Override 64 public View onCreateView( 65 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 66 mContainer = inflater.inflate(R.layout.fragment_pick, container, false); 67 68 mPick = (Button) mContainer.findViewById(android.R.id.button1); 69 mPick.setOnClickListener(mPickListener); 70 71 mCancel = (Button) mContainer.findViewById(android.R.id.button2); 72 mCancel.setOnClickListener(mCancelListener); 73 74 updateView(); 75 return mContainer; 76 } 77 78 private View.OnClickListener mPickListener = new View.OnClickListener() { 79 @Override 80 public void onClick(View v) { 81 final DocumentsActivity activity = DocumentsActivity.get(PickFragment.this); 82 activity.onPickRequested(mPickTarget); 83 } 84 }; 85 86 private View.OnClickListener mCancelListener = new View.OnClickListener() { 87 @Override 88 public void onClick(View v) { 89 final BaseActivity activity = BaseActivity.get(PickFragment.this); 90 activity.setResult(Activity.RESULT_CANCELED); 91 activity.finish(); 92 } 93 }; 94 95 /** 96 * @param action Which action defined in BaseActivity.State is the picker shown for. 97 */ 98 public void setPickTarget(int action, DocumentInfo pickTarget) { 99 mAction = action; 100 mPickTarget = pickTarget; 101 if (mContainer != null) { 102 updateView(); 103 } 104 } 105 106 /** 107 * Applies the state of fragment to the view components. 108 */ 109 private void updateView() { 110 switch (mAction) { 111 case BaseActivity.State.ACTION_OPEN_TREE: 112 mPick.setText(R.string.button_select); 113 mCancel.setVisibility(View.GONE); 114 break; 115 case BaseActivity.State.ACTION_OPEN_COPY_DESTINATION: 116 mPick.setText(R.string.button_copy); 117 mCancel.setVisibility(View.VISIBLE); 118 break; 119 default: 120 mContainer.setVisibility(View.GONE); 121 return; 122 } 123 124 if (mPickTarget != null && ( 125 mAction == BaseActivity.State.ACTION_OPEN_TREE || 126 mPickTarget.isCreateSupported())) { 127 mContainer.setVisibility(View.VISIBLE); 128 } else { 129 mContainer.setVisibility(View.GONE); 130 } 131 } 132} 133