blob: ce98db244270a93bbde9463382c4aa0114fa23bc [file] [log] [blame]
Jeff Sharkey54e55b72013-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
17package com.android.documentsui;
18
19import android.app.Fragment;
20import android.app.FragmentManager;
21import android.app.FragmentTransaction;
22import android.content.Context;
23import android.os.Bundle;
Jeff Sharkey9fb567b2013-08-07 16:22:02 -070024import android.text.Editable;
25import android.text.TextWatcher;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070026import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.Button;
30import android.widget.EditText;
31import android.widget.ImageView;
Jeff Sharkey04d45a02013-10-23 15:46:38 -070032import android.widget.ProgressBar;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070033
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070034import com.android.documentsui.model.DocumentInfo;
Jeff Sharkey9fb567b2013-08-07 16:22:02 -070035
Jeff Sharkey54e55b72013-06-30 20:02:59 -070036/**
37 * Display document title editor and save button.
38 */
39public class SaveFragment extends Fragment {
40 public static final String TAG = "SaveFragment";
41
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070042 private DocumentInfo mReplaceTarget;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070043 private EditText mDisplayName;
44 private Button mSave;
Jeff Sharkey04d45a02013-10-23 15:46:38 -070045 private ProgressBar mProgress;
Jeff Sharkey9fb567b2013-08-07 16:22:02 -070046 private boolean mIgnoreNextEdit;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070047
48 private static final String EXTRA_MIME_TYPE = "mime_type";
49 private static final String EXTRA_DISPLAY_NAME = "display_name";
50
51 public static void show(FragmentManager fm, String mimeType, String displayName) {
52 final Bundle args = new Bundle();
53 args.putString(EXTRA_MIME_TYPE, mimeType);
54 args.putString(EXTRA_DISPLAY_NAME, displayName);
55
56 final SaveFragment fragment = new SaveFragment();
57 fragment.setArguments(args);
58
59 final FragmentTransaction ft = fm.beginTransaction();
Jeff Sharkey66516692013-08-06 11:26:10 -070060 ft.replace(R.id.container_save, fragment, TAG);
Jeff Sharkey54e55b72013-06-30 20:02:59 -070061 ft.commitAllowingStateLoss();
62 }
63
64 public static SaveFragment get(FragmentManager fm) {
65 return (SaveFragment) fm.findFragmentByTag(TAG);
66 }
67
68 @Override
69 public View onCreateView(
70 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
71 final Context context = inflater.getContext();
72
73 final View view = inflater.inflate(R.layout.fragment_save, container, false);
74
75 final ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -070076 icon.setImageDrawable(
Jeff Sharkey0b14db32013-09-04 18:03:18 -070077 IconUtils.loadMimeIcon(context, getArguments().getString(EXTRA_MIME_TYPE)));
Jeff Sharkey54e55b72013-06-30 20:02:59 -070078
79 mDisplayName = (EditText) view.findViewById(android.R.id.title);
Jeff Sharkey9fb567b2013-08-07 16:22:02 -070080 mDisplayName.addTextChangedListener(mDisplayNameWatcher);
Jeff Sharkey54e55b72013-06-30 20:02:59 -070081 mDisplayName.setText(getArguments().getString(EXTRA_DISPLAY_NAME));
82
83 mSave = (Button) view.findViewById(android.R.id.button1);
84 mSave.setOnClickListener(mSaveListener);
85 mSave.setEnabled(false);
86
Jeff Sharkey04d45a02013-10-23 15:46:38 -070087 mProgress = (ProgressBar) view.findViewById(android.R.id.progress);
88
Jeff Sharkey54e55b72013-06-30 20:02:59 -070089 return view;
90 }
91
Jeff Sharkey9fb567b2013-08-07 16:22:02 -070092 private TextWatcher mDisplayNameWatcher = new TextWatcher() {
Jeff Sharkey54e55b72013-06-30 20:02:59 -070093 @Override
Jeff Sharkey9fb567b2013-08-07 16:22:02 -070094 public void onTextChanged(CharSequence s, int start, int before, int count) {
95 if (mIgnoreNextEdit) {
96 mIgnoreNextEdit = false;
97 } else {
Jeff Sharkey9fb567b2013-08-07 16:22:02 -070098 mReplaceTarget = null;
99 }
100 }
101
102 @Override
103 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
104 // ignored
105 }
106
107 @Override
108 public void afterTextChanged(Editable s) {
109 // ignored
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700110 }
111 };
112
Jeff Sharkey9fb567b2013-08-07 16:22:02 -0700113 private View.OnClickListener mSaveListener = new View.OnClickListener() {
114 @Override
115 public void onClick(View v) {
116 final DocumentsActivity activity = DocumentsActivity.get(SaveFragment.this);
117 if (mReplaceTarget != null) {
118 activity.onSaveRequested(mReplaceTarget);
119 } else {
120 final String mimeType = getArguments().getString(EXTRA_MIME_TYPE);
121 final String displayName = mDisplayName.getText().toString();
122 activity.onSaveRequested(mimeType, displayName);
123 }
124 }
125 };
126
127 /**
128 * Set given document as target for in-place writing if user hits save
129 * without changing the filename. Can be set to {@code null} if user
130 * navigates outside the target directory.
131 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700132 public void setReplaceTarget(DocumentInfo replaceTarget) {
Jeff Sharkey9fb567b2013-08-07 16:22:02 -0700133 mReplaceTarget = replaceTarget;
134
135 if (mReplaceTarget != null) {
136 getArguments().putString(EXTRA_DISPLAY_NAME, replaceTarget.displayName);
137 mIgnoreNextEdit = true;
138 mDisplayName.setText(replaceTarget.displayName);
139 }
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700140 }
141
142 public void setSaveEnabled(boolean enabled) {
143 mSave.setEnabled(enabled);
144 }
Jeff Sharkey04d45a02013-10-23 15:46:38 -0700145
146 public void setPending(boolean pending) {
147 mSave.setVisibility(pending ? View.INVISIBLE : View.VISIBLE);
148 mProgress.setVisibility(pending ? View.VISIBLE : View.GONE);
149 }
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700150}