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 android.app.Fragment;
20 import android.app.FragmentManager;
21 import android.app.FragmentTransaction;
22 import android.os.Bundle;
23 import android.text.TextUtils;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.Button;
28 
29 import com.android.documentsui.model.DocumentInfo;
30 
31 import java.util.Locale;
32 
33 /**
34  * Display pick confirmation bar, usually for selecting a directory.
35  */
36 public class PickFragment extends Fragment {
37     public static final String TAG = "PickFragment";
38 
39     private DocumentInfo mPickTarget;
40 
41     private View mContainer;
42     private Button mPick;
43 
show(FragmentManager fm)44     public static void show(FragmentManager fm) {
45         final PickFragment fragment = new PickFragment();
46 
47         final FragmentTransaction ft = fm.beginTransaction();
48         ft.replace(R.id.container_save, fragment, TAG);
49         ft.commitAllowingStateLoss();
50     }
51 
get(FragmentManager fm)52     public static PickFragment get(FragmentManager fm) {
53         return (PickFragment) fm.findFragmentByTag(TAG);
54     }
55 
56     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)57     public View onCreateView(
58             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
59         mContainer = inflater.inflate(R.layout.fragment_pick, container, false);
60 
61         mPick = (Button) mContainer.findViewById(android.R.id.button1);
62         mPick.setOnClickListener(mPickListener);
63 
64         setPickTarget(null, null);
65 
66         return mContainer;
67     }
68 
69     private View.OnClickListener mPickListener = new View.OnClickListener() {
70         @Override
71         public void onClick(View v) {
72             final DocumentsActivity activity = DocumentsActivity.get(PickFragment.this);
73             activity.onPickRequested(mPickTarget);
74         }
75     };
76 
setPickTarget(DocumentInfo pickTarget, CharSequence displayName)77     public void setPickTarget(DocumentInfo pickTarget, CharSequence displayName) {
78         mPickTarget = pickTarget;
79 
80         if (mContainer != null) {
81             if (mPickTarget != null) {
82                 mContainer.setVisibility(View.VISIBLE);
83                 final Locale locale = getResources().getConfiguration().locale;
84                 final String raw = getString(R.string.menu_select).toUpperCase(locale);
85                 mPick.setText(TextUtils.expandTemplate(raw, displayName));
86             } else {
87                 mContainer.setVisibility(View.GONE);
88             }
89         }
90     }
91 }
92