1 /*
2  * Copyright (C) 2013 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.content.Context;
23 import android.os.Bundle;
24 import android.text.Editable;
25 import android.text.TextWatcher;
26 import android.view.KeyEvent;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.Button;
31 import android.widget.EditText;
32 import android.widget.ImageView;
33 import android.widget.ProgressBar;
34 
35 import com.android.documentsui.model.DocumentInfo;
36 
37 /**
38  * Display document title editor and save button.
39  */
40 public class SaveFragment extends Fragment {
41     public static final String TAG = "SaveFragment";
42 
43     private DocumentInfo mReplaceTarget;
44     private EditText mDisplayName;
45     private Button mSave;
46     private ProgressBar mProgress;
47     private boolean mIgnoreNextEdit;
48 
49     private static final String EXTRA_MIME_TYPE = "mime_type";
50     private static final String EXTRA_DISPLAY_NAME = "display_name";
51 
show(FragmentManager fm, String mimeType, String displayName)52     public static void show(FragmentManager fm, String mimeType, String displayName) {
53         final Bundle args = new Bundle();
54         args.putString(EXTRA_MIME_TYPE, mimeType);
55         args.putString(EXTRA_DISPLAY_NAME, displayName);
56 
57         final SaveFragment fragment = new SaveFragment();
58         fragment.setArguments(args);
59 
60         final FragmentTransaction ft = fm.beginTransaction();
61         ft.replace(R.id.container_save, fragment, TAG);
62         ft.commitAllowingStateLoss();
63     }
64 
get(FragmentManager fm)65     public static SaveFragment get(FragmentManager fm) {
66         return (SaveFragment) fm.findFragmentByTag(TAG);
67     }
68 
69     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)70     public View onCreateView(
71             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
72         final Context context = inflater.getContext();
73 
74         final View view = inflater.inflate(R.layout.fragment_save, container, false);
75 
76         final ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
77         icon.setImageDrawable(
78                 IconUtils.loadMimeIcon(context, getArguments().getString(EXTRA_MIME_TYPE)));
79 
80         mDisplayName = (EditText) view.findViewById(android.R.id.title);
81         mDisplayName.addTextChangedListener(mDisplayNameWatcher);
82         mDisplayName.setText(getArguments().getString(EXTRA_DISPLAY_NAME));
83         mDisplayName.setOnKeyListener(
84                 new View.OnKeyListener() {
85                     @Override
86                     public boolean onKey(View v, int keyCode, KeyEvent event) {
87                         // Only handle key-down events. This is simpler, consistent with most other
88                         // UIs, and enables the handling of repeated key events from holding down a
89                         // key.
90                         if (event.getAction() != KeyEvent.ACTION_DOWN) {
91                             return false;
92                         }
93 
94                         if (keyCode == KeyEvent.KEYCODE_ENTER && mSave.isEnabled()) {
95                             performSave();
96                             return true;
97                         }
98                         return false;
99                     }
100                 });
101 
102         mSave = (Button) view.findViewById(android.R.id.button1);
103         mSave.setOnClickListener(mSaveListener);
104         mSave.setEnabled(false);
105 
106         mProgress = (ProgressBar) view.findViewById(android.R.id.progress);
107 
108         return view;
109     }
110 
111     private TextWatcher mDisplayNameWatcher = new TextWatcher() {
112         @Override
113         public void onTextChanged(CharSequence s, int start, int before, int count) {
114             if (mIgnoreNextEdit) {
115                 mIgnoreNextEdit = false;
116             } else {
117                 mReplaceTarget = null;
118             }
119         }
120 
121         @Override
122         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
123             // ignored
124         }
125 
126         @Override
127         public void afterTextChanged(Editable s) {
128             // ignored
129         }
130     };
131 
132     private View.OnClickListener mSaveListener = new View.OnClickListener() {
133         @Override
134         public void onClick(View v) {
135             performSave();
136         }
137 
138     };
139 
performSave()140     private void performSave() {
141         final DocumentsActivity activity = DocumentsActivity.get(SaveFragment.this);
142         if (mReplaceTarget != null) {
143             activity.onSaveRequested(mReplaceTarget);
144         } else {
145             final String mimeType = getArguments().getString(EXTRA_MIME_TYPE);
146             final String displayName = mDisplayName.getText().toString();
147             activity.onSaveRequested(mimeType, displayName);
148         }
149     }
150 
151     /**
152      * Set given document as target for in-place writing if user hits save
153      * without changing the filename. Can be set to {@code null} if user
154      * navigates outside the target directory.
155      */
setReplaceTarget(DocumentInfo replaceTarget)156     public void setReplaceTarget(DocumentInfo replaceTarget) {
157         mReplaceTarget = replaceTarget;
158 
159         if (mReplaceTarget != null) {
160             getArguments().putString(EXTRA_DISPLAY_NAME, replaceTarget.displayName);
161             mIgnoreNextEdit = true;
162             mDisplayName.setText(replaceTarget.displayName);
163         }
164     }
165 
prepareForDirectory(DocumentInfo cwd)166     public void prepareForDirectory(DocumentInfo cwd) {
167         setSaveEnabled(cwd != null && cwd.isCreateSupported());
168     }
169 
setSaveEnabled(boolean enabled)170     private void setSaveEnabled(boolean enabled) {
171         mSave.setEnabled(enabled);
172     }
173 
setPending(boolean pending)174     public void setPending(boolean pending) {
175         mSave.setVisibility(pending ? View.INVISIBLE : View.VISIBLE);
176         mProgress.setVisibility(pending ? View.VISIBLE : View.GONE);
177     }
178 }
179