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.app.Fragment;
20import android.app.FragmentManager;
21import android.app.FragmentTransaction;
22import android.os.Bundle;
23import android.text.TextUtils;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.Button;
28
29import com.android.documentsui.model.DocumentInfo;
30
31import java.util.Locale;
32
33/**
34 * Display pick confirmation bar, usually for selecting a directory.
35 */
36public 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
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
52    public static PickFragment get(FragmentManager fm) {
53        return (PickFragment) fm.findFragmentByTag(TAG);
54    }
55
56    @Override
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
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