blob: 9d70c5187bd7e2abb4c5219d5ebfb677ccfc4267 [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;
26import android.util.Log;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070027import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.Button;
31import android.widget.EditText;
32import android.widget.ImageView;
Jeff Sharkey04d45a02013-10-23 15:46:38 -070033import android.widget.ProgressBar;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070034
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070035import com.android.documentsui.model.DocumentInfo;
Jeff Sharkey9fb567b2013-08-07 16:22:02 -070036
Jeff Sharkey54e55b72013-06-30 20:02:59 -070037/**
38 * Display document title editor and save button.
39 */
40public class SaveFragment extends Fragment {
41 public static final String TAG = "SaveFragment";
42
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -070043 private DocumentInfo mReplaceTarget;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070044 private EditText mDisplayName;
45 private Button mSave;
Jeff Sharkey04d45a02013-10-23 15:46:38 -070046 private ProgressBar mProgress;
Jeff Sharkey9fb567b2013-08-07 16:22:02 -070047 private boolean mIgnoreNextEdit;
Jeff Sharkey54e55b72013-06-30 20:02:59 -070048
49 private static final String EXTRA_MIME_TYPE = "mime_type";
50 private static final String EXTRA_DISPLAY_NAME = "display_name";
51
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();
Jeff Sharkey66516692013-08-06 11:26:10 -070061 ft.replace(R.id.container_save, fragment, TAG);
Jeff Sharkey54e55b72013-06-30 20:02:59 -070062 ft.commitAllowingStateLoss();
63 }
64
65 public static SaveFragment get(FragmentManager fm) {
66 return (SaveFragment) fm.findFragmentByTag(TAG);
67 }
68
69 @Override
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);
Jeff Sharkeyaeb16e22013-08-27 18:26:48 -070077 icon.setImageDrawable(
Jeff Sharkey0b14db32013-09-04 18:03:18 -070078 IconUtils.loadMimeIcon(context, getArguments().getString(EXTRA_MIME_TYPE)));
Jeff Sharkey54e55b72013-06-30 20:02:59 -070079
80 mDisplayName = (EditText) view.findViewById(android.R.id.title);
Jeff Sharkey9fb567b2013-08-07 16:22:02 -070081 mDisplayName.addTextChangedListener(mDisplayNameWatcher);
Jeff Sharkey54e55b72013-06-30 20:02:59 -070082 mDisplayName.setText(getArguments().getString(EXTRA_DISPLAY_NAME));
83
84 mSave = (Button) view.findViewById(android.R.id.button1);
85 mSave.setOnClickListener(mSaveListener);
86 mSave.setEnabled(false);
87
Jeff Sharkey04d45a02013-10-23 15:46:38 -070088 mProgress = (ProgressBar) view.findViewById(android.R.id.progress);
89
Jeff Sharkey54e55b72013-06-30 20:02:59 -070090 return view;
91 }
92
Jeff Sharkey9fb567b2013-08-07 16:22:02 -070093 private TextWatcher mDisplayNameWatcher = new TextWatcher() {
Jeff Sharkey54e55b72013-06-30 20:02:59 -070094 @Override
Jeff Sharkey9fb567b2013-08-07 16:22:02 -070095 public void onTextChanged(CharSequence s, int start, int before, int count) {
96 if (mIgnoreNextEdit) {
97 mIgnoreNextEdit = false;
98 } else {
Jeff Sharkey9fb567b2013-08-07 16:22:02 -070099 mReplaceTarget = null;
100 }
101 }
102
103 @Override
104 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
105 // ignored
106 }
107
108 @Override
109 public void afterTextChanged(Editable s) {
110 // ignored
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700111 }
112 };
113
Jeff Sharkey9fb567b2013-08-07 16:22:02 -0700114 private View.OnClickListener mSaveListener = new View.OnClickListener() {
115 @Override
116 public void onClick(View v) {
117 final DocumentsActivity activity = DocumentsActivity.get(SaveFragment.this);
118 if (mReplaceTarget != null) {
119 activity.onSaveRequested(mReplaceTarget);
120 } else {
121 final String mimeType = getArguments().getString(EXTRA_MIME_TYPE);
122 final String displayName = mDisplayName.getText().toString();
123 activity.onSaveRequested(mimeType, displayName);
124 }
125 }
126 };
127
128 /**
129 * Set given document as target for in-place writing if user hits save
130 * without changing the filename. Can be set to {@code null} if user
131 * navigates outside the target directory.
132 */
Jeff Sharkeyae9b51b2013-08-31 15:02:20 -0700133 public void setReplaceTarget(DocumentInfo replaceTarget) {
Jeff Sharkey9fb567b2013-08-07 16:22:02 -0700134 mReplaceTarget = replaceTarget;
135
136 if (mReplaceTarget != null) {
137 getArguments().putString(EXTRA_DISPLAY_NAME, replaceTarget.displayName);
138 mIgnoreNextEdit = true;
139 mDisplayName.setText(replaceTarget.displayName);
140 }
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700141 }
142
143 public void setSaveEnabled(boolean enabled) {
144 mSave.setEnabled(enabled);
145 }
Jeff Sharkey04d45a02013-10-23 15:46:38 -0700146
147 public void setPending(boolean pending) {
148 mSave.setVisibility(pending ? View.INVISIBLE : View.VISIBLE);
149 mProgress.setVisibility(pending ? View.VISIBLE : View.GONE);
150 }
Jeff Sharkey54e55b72013-06-30 20:02:59 -0700151}