Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 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; |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 24 | import android.text.Editable; |
| 25 | import android.text.TextWatcher; |
Steve McKay | 0c2a0a8 | 2015-10-14 17:41:13 -0700 | [diff] [blame] | 26 | import android.view.KeyEvent; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 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; |
Jeff Sharkey | 04d45a0 | 2013-10-23 15:46:38 -0700 | [diff] [blame] | 33 | import android.widget.ProgressBar; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 34 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 35 | import com.android.documentsui.model.DocumentInfo; |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 36 | |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Display document title editor and save button. |
| 39 | */ |
| 40 | public class SaveFragment extends Fragment { |
| 41 | public static final String TAG = "SaveFragment"; |
| 42 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 43 | private DocumentInfo mReplaceTarget; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 44 | private EditText mDisplayName; |
| 45 | private Button mSave; |
Jeff Sharkey | 04d45a0 | 2013-10-23 15:46:38 -0700 | [diff] [blame] | 46 | private ProgressBar mProgress; |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 47 | private boolean mIgnoreNextEdit; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 48 | |
| 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 Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 61 | ft.replace(R.id.container_save, fragment, TAG); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 62 | 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 Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 77 | icon.setImageDrawable( |
Jeff Sharkey | 0b14db3 | 2013-09-04 18:03:18 -0700 | [diff] [blame] | 78 | IconUtils.loadMimeIcon(context, getArguments().getString(EXTRA_MIME_TYPE))); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 79 | |
| 80 | mDisplayName = (EditText) view.findViewById(android.R.id.title); |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 81 | mDisplayName.addTextChangedListener(mDisplayNameWatcher); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 82 | mDisplayName.setText(getArguments().getString(EXTRA_DISPLAY_NAME)); |
Steve McKay | 0c2a0a8 | 2015-10-14 17:41:13 -0700 | [diff] [blame] | 83 | mDisplayName.setOnKeyListener( |
| 84 | new View.OnKeyListener() { |
| 85 | @Override |
| 86 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 87 | if (keyCode == KeyEvent.KEYCODE_ENTER && mSave.isEnabled()) { |
| 88 | performSave(); |
| 89 | return true; |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | }); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 94 | |
| 95 | mSave = (Button) view.findViewById(android.R.id.button1); |
| 96 | mSave.setOnClickListener(mSaveListener); |
| 97 | mSave.setEnabled(false); |
| 98 | |
Jeff Sharkey | 04d45a0 | 2013-10-23 15:46:38 -0700 | [diff] [blame] | 99 | mProgress = (ProgressBar) view.findViewById(android.R.id.progress); |
| 100 | |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 101 | return view; |
| 102 | } |
| 103 | |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 104 | private TextWatcher mDisplayNameWatcher = new TextWatcher() { |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 105 | @Override |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 106 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 107 | if (mIgnoreNextEdit) { |
| 108 | mIgnoreNextEdit = false; |
| 109 | } else { |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 110 | mReplaceTarget = null; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 116 | // ignored |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void afterTextChanged(Editable s) { |
| 121 | // ignored |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 122 | } |
| 123 | }; |
| 124 | |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 125 | private View.OnClickListener mSaveListener = new View.OnClickListener() { |
| 126 | @Override |
| 127 | public void onClick(View v) { |
Steve McKay | 0c2a0a8 | 2015-10-14 17:41:13 -0700 | [diff] [blame] | 128 | performSave(); |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 129 | } |
Steve McKay | 0c2a0a8 | 2015-10-14 17:41:13 -0700 | [diff] [blame] | 130 | |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
Steve McKay | 0c2a0a8 | 2015-10-14 17:41:13 -0700 | [diff] [blame] | 133 | private void performSave() { |
| 134 | final DocumentsActivity activity = DocumentsActivity.get(SaveFragment.this); |
| 135 | if (mReplaceTarget != null) { |
| 136 | activity.onSaveRequested(mReplaceTarget); |
| 137 | } else { |
| 138 | final String mimeType = getArguments().getString(EXTRA_MIME_TYPE); |
| 139 | final String displayName = mDisplayName.getText().toString(); |
| 140 | activity.onSaveRequested(mimeType, displayName); |
| 141 | } |
| 142 | } |
| 143 | |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Set given document as target for in-place writing if user hits save |
| 146 | * without changing the filename. Can be set to {@code null} if user |
| 147 | * navigates outside the target directory. |
| 148 | */ |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 149 | public void setReplaceTarget(DocumentInfo replaceTarget) { |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 150 | mReplaceTarget = replaceTarget; |
| 151 | |
| 152 | if (mReplaceTarget != null) { |
| 153 | getArguments().putString(EXTRA_DISPLAY_NAME, replaceTarget.displayName); |
| 154 | mIgnoreNextEdit = true; |
| 155 | mDisplayName.setText(replaceTarget.displayName); |
| 156 | } |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Steve McKay | 0c2a0a8 | 2015-10-14 17:41:13 -0700 | [diff] [blame] | 159 | public void prepareForDirectory(DocumentInfo cwd) { |
| 160 | setSaveEnabled(cwd != null && cwd.isCreateSupported()); |
| 161 | } |
| 162 | |
| 163 | private void setSaveEnabled(boolean enabled) { |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 164 | mSave.setEnabled(enabled); |
| 165 | } |
Jeff Sharkey | 04d45a0 | 2013-10-23 15:46:38 -0700 | [diff] [blame] | 166 | |
| 167 | public void setPending(boolean pending) { |
| 168 | mSave.setVisibility(pending ? View.INVISIBLE : View.VISIBLE); |
| 169 | mProgress.setVisibility(pending ? View.VISIBLE : View.GONE); |
| 170 | } |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 171 | } |