blob: e013c19f6eb887fa5778ac710f4fcfa8d5b7e649 [file] [log] [blame]
Jeff Sharkey09c10bf2013-06-30 20:02:59 -07001/*
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
Steve McKay16e0c1f2016-09-15 12:41:13 -070017package com.android.documentsui.picker;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070018
19import android.app.Fragment;
20import android.app.FragmentManager;
21import android.app.FragmentTransaction;
22import android.content.Context;
23import android.os.Bundle;
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070024import android.text.Editable;
Ben Lin819940f2016-07-11 16:25:33 -070025import android.text.TextUtils;
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070026import android.text.TextWatcher;
Steve McKay0fe95042015-10-14 17:41:13 -070027import android.view.KeyEvent;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070028import android.view.LayoutInflater;
29import android.view.View;
30import android.view.ViewGroup;
31import android.widget.Button;
32import android.widget.EditText;
33import android.widget.ImageView;
Jeff Sharkey4be51f12013-10-23 15:46:38 -070034import android.widget.ProgressBar;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070035
Steve McKay16e0c1f2016-09-15 12:41:13 -070036import com.android.documentsui.IconUtils;
Garfield Tan23ac60c2017-03-13 17:40:43 -070037import com.android.documentsui.Injector;
Steve McKay16e0c1f2016-09-15 12:41:13 -070038import com.android.documentsui.R;
Garfield Tan23ac60c2017-03-13 17:40:43 -070039import com.android.documentsui.base.BooleanConsumer;
Steve McKayd0805062016-09-15 14:30:38 -070040import com.android.documentsui.base.DocumentInfo;
Garfield Tan23ac60c2017-03-13 17:40:43 -070041import com.android.documentsui.base.Shared;
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070042
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070043/**
44 * Display document title editor and save button.
45 */
Steve McKay8d79f252016-10-13 10:50:04 -070046public class SaveFragment extends Fragment {
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070047 public static final String TAG = "SaveFragment";
48
Garfield Tan23ac60c2017-03-13 17:40:43 -070049 private final BooleanConsumer mInProgressStateListener = this::setPending;
50
51 private Injector<ActionHandler<PickActivity>> mInjector;
Jeff Sharkey724deeb2013-08-31 15:02:20 -070052 private DocumentInfo mReplaceTarget;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070053 private EditText mDisplayName;
54 private Button mSave;
Jeff Sharkey4be51f12013-10-23 15:46:38 -070055 private ProgressBar mProgress;
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070056 private boolean mIgnoreNextEdit;
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070057
58 private static final String EXTRA_MIME_TYPE = "mime_type";
59 private static final String EXTRA_DISPLAY_NAME = "display_name";
60
Steve McKay16e0c1f2016-09-15 12:41:13 -070061 static void show(FragmentManager fm, String mimeType, String displayName) {
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070062 final Bundle args = new Bundle();
63 args.putString(EXTRA_MIME_TYPE, mimeType);
64 args.putString(EXTRA_DISPLAY_NAME, displayName);
65
66 final SaveFragment fragment = new SaveFragment();
67 fragment.setArguments(args);
68
69 final FragmentTransaction ft = fm.beginTransaction();
Jeff Sharkey76112212013-08-06 11:26:10 -070070 ft.replace(R.id.container_save, fragment, TAG);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070071 ft.commitAllowingStateLoss();
72 }
73
74 public static SaveFragment get(FragmentManager fm) {
75 return (SaveFragment) fm.findFragmentByTag(TAG);
76 }
77
78 @Override
79 public View onCreateView(
80 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
81 final Context context = inflater.getContext();
82
83 final View view = inflater.inflate(R.layout.fragment_save, container, false);
84
85 final ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
Jeff Sharkeyb6a7f2c2013-08-27 18:26:48 -070086 icon.setImageDrawable(
Jeff Sharkey5629ec52013-09-04 18:03:18 -070087 IconUtils.loadMimeIcon(context, getArguments().getString(EXTRA_MIME_TYPE)));
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070088
89 mDisplayName = (EditText) view.findViewById(android.R.id.title);
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -070090 mDisplayName.addTextChangedListener(mDisplayNameWatcher);
Jeff Sharkey09c10bf2013-06-30 20:02:59 -070091 mDisplayName.setText(getArguments().getString(EXTRA_DISPLAY_NAME));
Steve McKay0fe95042015-10-14 17:41:13 -070092 mDisplayName.setOnKeyListener(
93 new View.OnKeyListener() {
94 @Override
95 public boolean onKey(View v, int keyCode, KeyEvent event) {
Daichi Hirono2dfba672016-04-14 14:40:47 +090096 // Only handle key-down events. This is simpler, consistent with most other
97 // UIs, and enables the handling of repeated key events from holding down a
98 // key.
99 if (event.getAction() != KeyEvent.ACTION_DOWN) {
100 return false;
101 }
102
Ben Lin819940f2016-07-11 16:25:33 -0700103 // Returning false in this method will bubble the event up to
104 // {@link BaseActivity#onKeyDown}. In order to prevent backspace popping
105 // documents once the textView is empty, we are going to trap it here.
106 if (keyCode == KeyEvent.KEYCODE_DEL
107 && TextUtils.isEmpty(mDisplayName.getText())) {
108 return true;
109 }
110
Steve McKay0fe95042015-10-14 17:41:13 -0700111 if (keyCode == KeyEvent.KEYCODE_ENTER && mSave.isEnabled()) {
112 performSave();
113 return true;
114 }
115 return false;
116 }
117 });
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700118
119 mSave = (Button) view.findViewById(android.R.id.button1);
120 mSave.setOnClickListener(mSaveListener);
121 mSave.setEnabled(false);
122
Jeff Sharkey4be51f12013-10-23 15:46:38 -0700123 mProgress = (ProgressBar) view.findViewById(android.R.id.progress);
124
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700125 return view;
126 }
127
Garfield Tan23ac60c2017-03-13 17:40:43 -0700128 @Override
129 public void onActivityCreated(Bundle savedInstanceState) {
130 super.onActivityCreated(savedInstanceState);
131 mInjector = ((PickActivity) getActivity()).getInjector();
132
133 if (savedInstanceState != null) {
134 mReplaceTarget = savedInstanceState.getParcelable(Shared.EXTRA_DOC);
135 }
136 }
137
138 @Override
139 public void onSaveInstanceState(Bundle outBundle) {
140 super.onSaveInstanceState(outBundle);
141
142 outBundle.putParcelable(Shared.EXTRA_DOC, mReplaceTarget);
143 }
144
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700145 private TextWatcher mDisplayNameWatcher = new TextWatcher() {
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700146 @Override
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700147 public void onTextChanged(CharSequence s, int start, int before, int count) {
148 if (mIgnoreNextEdit) {
149 mIgnoreNextEdit = false;
150 } else {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700151 mReplaceTarget = null;
152 }
153 }
154
155 @Override
156 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
157 // ignored
158 }
159
160 @Override
161 public void afterTextChanged(Editable s) {
162 // ignored
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700163 }
164 };
165
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700166 private View.OnClickListener mSaveListener = new View.OnClickListener() {
167 @Override
168 public void onClick(View v) {
Steve McKay0fe95042015-10-14 17:41:13 -0700169 performSave();
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700170 }
Steve McKay0fe95042015-10-14 17:41:13 -0700171
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700172 };
173
Steve McKay0fe95042015-10-14 17:41:13 -0700174 private void performSave() {
Steve McKay0fe95042015-10-14 17:41:13 -0700175 if (mReplaceTarget != null) {
Garfield Tan23ac60c2017-03-13 17:40:43 -0700176 mInjector.actions.saveDocument(getChildFragmentManager(), mReplaceTarget);
Steve McKay0fe95042015-10-14 17:41:13 -0700177 } else {
178 final String mimeType = getArguments().getString(EXTRA_MIME_TYPE);
179 final String displayName = mDisplayName.getText().toString();
Garfield Tan23ac60c2017-03-13 17:40:43 -0700180 mInjector.actions.saveDocument(mimeType, displayName, mInProgressStateListener);
Steve McKay0fe95042015-10-14 17:41:13 -0700181 }
182 }
183
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700184 /**
185 * Set given document as target for in-place writing if user hits save
186 * without changing the filename. Can be set to {@code null} if user
187 * navigates outside the target directory.
188 */
Jeff Sharkey724deeb2013-08-31 15:02:20 -0700189 public void setReplaceTarget(DocumentInfo replaceTarget) {
Jeff Sharkeyc6cbdf12013-08-07 16:22:02 -0700190 mReplaceTarget = replaceTarget;
191
192 if (mReplaceTarget != null) {
193 getArguments().putString(EXTRA_DISPLAY_NAME, replaceTarget.displayName);
194 mIgnoreNextEdit = true;
195 mDisplayName.setText(replaceTarget.displayName);
196 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700197 }
198
Steve McKay0fe95042015-10-14 17:41:13 -0700199 public void prepareForDirectory(DocumentInfo cwd) {
200 setSaveEnabled(cwd != null && cwd.isCreateSupported());
201 }
202
203 private void setSaveEnabled(boolean enabled) {
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700204 mSave.setEnabled(enabled);
205 }
Jeff Sharkey4be51f12013-10-23 15:46:38 -0700206
Garfield Tan23ac60c2017-03-13 17:40:43 -0700207 private void setPending(boolean pending) {
Jeff Sharkey4be51f12013-10-23 15:46:38 -0700208 mSave.setVisibility(pending ? View.INVISIBLE : View.VISIBLE);
209 mProgress.setVisibility(pending ? View.VISIBLE : View.GONE);
210 }
Jeff Sharkey09c10bf2013-06-30 20:02:59 -0700211}