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.picker;
18
19import static com.android.documentsui.services.FileOperationService.OPERATION_DELETE;
20import static com.android.documentsui.services.FileOperationService.OPERATION_COPY;
21import static com.android.documentsui.services.FileOperationService.OPERATION_COMPRESS;
22import static com.android.documentsui.services.FileOperationService.OPERATION_EXTRACT;
23import static com.android.documentsui.services.FileOperationService.OPERATION_MOVE;
24import static com.android.documentsui.services.FileOperationService.OPERATION_UNKNOWN;
25
26import android.app.Activity;
27import android.app.Fragment;
28import android.app.FragmentManager;
29import android.app.FragmentTransaction;
30import android.os.Bundle;
31import android.view.LayoutInflater;
32import android.view.View;
33import android.view.ViewGroup;
34import android.widget.Button;
35import android.widget.TextView;
36
37import com.android.documentsui.BaseActivity;
38import com.android.documentsui.Injector;
39import com.android.documentsui.R;
40import com.android.documentsui.base.DocumentInfo;
41import com.android.documentsui.base.State;
42import com.android.documentsui.services.FileOperationService.OpType;
43
44/**
45 * Display pick confirmation bar, usually for selecting a directory.
46 */
47public class PickFragment extends Fragment {
48    public static final String TAG = "PickFragment";
49
50    private static final String ACTION_KEY = "action";
51    private static final String COPY_OPERATION_SUBTYPE_KEY = "copyOperationSubType";
52    private static final String PICK_TARGET_KEY = "pickTarget";
53
54    private final View.OnClickListener mPickListener = new View.OnClickListener() {
55        @Override
56        public void onClick(View v) {
57            mInjector.actions.pickDocument(mPickTarget);
58        }
59    };
60
61    private final View.OnClickListener mCancelListener = new View.OnClickListener() {
62        @Override
63        public void onClick(View v) {
64            final BaseActivity activity = BaseActivity.get(PickFragment.this);
65            activity.setResult(Activity.RESULT_CANCELED);
66            activity.finish();
67        }
68    };
69
70    private Injector<ActionHandler<PickActivity>> mInjector;
71    private int mAction;
72    // Only legal values are OPERATION_COPY, OPERATION_COMPRESS, OPERATION_EXTRACT,
73    // OPERATION_MOVE, and unset (OPERATION_UNKNOWN).
74    private @OpType int mCopyOperationSubType = OPERATION_UNKNOWN;
75    private DocumentInfo mPickTarget;
76    private View mContainer;
77    private TextView mPick;
78    private TextView mCancel;
79
80    public static void show(FragmentManager fm) {
81        // Fragment can be restored by FragmentManager automatically.
82        if (get(fm) != null) {
83            return;
84        }
85
86        final PickFragment fragment = new PickFragment();
87        final FragmentTransaction ft = fm.beginTransaction();
88        ft.replace(R.id.container_save, fragment, TAG);
89        ft.commitAllowingStateLoss();
90    }
91
92    public static PickFragment get(FragmentManager fm) {
93        return (PickFragment) fm.findFragmentByTag(TAG);
94    }
95
96    @Override
97    public View onCreateView(
98            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
99        mContainer = inflater.inflate(R.layout.fragment_pick, container, false);
100
101        mPick = (TextView) mContainer.findViewById(android.R.id.button1);
102        mPick.setOnClickListener(mPickListener);
103
104        mCancel = (TextView) mContainer.findViewById(android.R.id.button2);
105        mCancel.setOnClickListener(mCancelListener);
106
107        updateView();
108        return mContainer;
109    }
110
111    @Override
112    public void onActivityCreated(Bundle savedInstanceState) {
113        super.onActivityCreated(savedInstanceState);
114        if (savedInstanceState != null) {
115            // Restore status
116            mAction = savedInstanceState.getInt(ACTION_KEY);
117            mCopyOperationSubType =
118                    savedInstanceState.getInt(COPY_OPERATION_SUBTYPE_KEY);
119            mPickTarget = savedInstanceState.getParcelable(PICK_TARGET_KEY);
120            updateView();
121        }
122
123        mInjector = ((PickActivity) getActivity()).getInjector();
124    }
125
126    @Override
127    public void onSaveInstanceState(final Bundle outState) {
128        super.onSaveInstanceState(outState);
129        outState.putInt(ACTION_KEY, mAction);
130        outState.putInt(COPY_OPERATION_SUBTYPE_KEY, mCopyOperationSubType);
131        outState.putParcelable(PICK_TARGET_KEY, mPickTarget);
132    }
133
134    /**
135     * @param action Which action defined in State is the picker shown for.
136     */
137    public void setPickTarget(
138            int action, @OpType int copyOperationSubType, DocumentInfo pickTarget) {
139        assert(copyOperationSubType != OPERATION_DELETE);
140
141        mAction = action;
142        mCopyOperationSubType = copyOperationSubType;
143        mPickTarget = pickTarget;
144        if (mContainer != null) {
145            updateView();
146        }
147    }
148
149    /**
150     * Applies the state of fragment to the view components.
151     */
152    private void updateView() {
153        switch (mAction) {
154            case State.ACTION_OPEN_TREE:
155                mPick.setText(R.string.button_select);
156                mCancel.setVisibility(View.GONE);
157                break;
158            case State.ACTION_PICK_COPY_DESTINATION:
159                int titleId;
160                switch (mCopyOperationSubType) {
161                    case OPERATION_COPY:
162                        titleId = R.string.button_copy;
163                        break;
164                    case OPERATION_COMPRESS:
165                        titleId = R.string.button_compress;
166                        break;
167                    case OPERATION_EXTRACT:
168                        titleId = R.string.button_extract;
169                        break;
170                    case OPERATION_MOVE:
171                        titleId = R.string.button_move;
172                        break;
173                    default:
174                        throw new UnsupportedOperationException();
175                }
176                mPick.setText(titleId);
177                mCancel.setVisibility(View.VISIBLE);
178                break;
179            default:
180                mContainer.setVisibility(View.GONE);
181                return;
182        }
183
184        if (mPickTarget != null && (
185                mAction == State.ACTION_OPEN_TREE ||
186                mPickTarget.isCreateSupported())) {
187            mContainer.setVisibility(View.VISIBLE);
188        } else {
189            mContainer.setVisibility(View.GONE);
190        }
191    }
192}
193