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; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 26 | import android.view.LayoutInflater; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.widget.Button; |
| 30 | import android.widget.EditText; |
| 31 | import android.widget.ImageView; |
Jeff Sharkey | 04d45a0 | 2013-10-23 15:46:38 -0700 | [diff] [blame] | 32 | import android.widget.ProgressBar; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 33 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 34 | import com.android.documentsui.model.DocumentInfo; |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 35 | |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Display document title editor and save button. |
| 38 | */ |
| 39 | public class SaveFragment extends Fragment { |
| 40 | public static final String TAG = "SaveFragment"; |
| 41 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 42 | private DocumentInfo mReplaceTarget; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 43 | private EditText mDisplayName; |
| 44 | private Button mSave; |
Jeff Sharkey | 04d45a0 | 2013-10-23 15:46:38 -0700 | [diff] [blame] | 45 | private ProgressBar mProgress; |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 46 | private boolean mIgnoreNextEdit; |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 47 | |
| 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 Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 60 | ft.replace(R.id.container_save, fragment, TAG); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 61 | 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 Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 76 | icon.setImageDrawable( |
Jeff Sharkey | 0b14db3 | 2013-09-04 18:03:18 -0700 | [diff] [blame] | 77 | IconUtils.loadMimeIcon(context, getArguments().getString(EXTRA_MIME_TYPE))); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 78 | |
| 79 | mDisplayName = (EditText) view.findViewById(android.R.id.title); |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 80 | mDisplayName.addTextChangedListener(mDisplayNameWatcher); |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 81 | 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 Sharkey | 04d45a0 | 2013-10-23 15:46:38 -0700 | [diff] [blame] | 87 | mProgress = (ProgressBar) view.findViewById(android.R.id.progress); |
| 88 | |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 89 | return view; |
| 90 | } |
| 91 | |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 92 | private TextWatcher mDisplayNameWatcher = new TextWatcher() { |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 93 | @Override |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 94 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 95 | if (mIgnoreNextEdit) { |
| 96 | mIgnoreNextEdit = false; |
| 97 | } else { |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 98 | 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 Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 110 | } |
| 111 | }; |
| 112 | |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 113 | 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 Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 132 | public void setReplaceTarget(DocumentInfo replaceTarget) { |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 133 | 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 Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | public void setSaveEnabled(boolean enabled) { |
| 143 | mSave.setEnabled(enabled); |
| 144 | } |
Jeff Sharkey | 04d45a0 | 2013-10-23 15:46:38 -0700 | [diff] [blame] | 145 | |
| 146 | public void setPending(boolean pending) { |
| 147 | mSave.setVisibility(pending ? View.INVISIBLE : View.VISIBLE); |
| 148 | mProgress.setVisibility(pending ? View.VISIBLE : View.GONE); |
| 149 | } |
Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 150 | } |