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; 24 import android.text.Editable; 25 import android.text.TextWatcher; 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; 32 import android.widget.ProgressBar; 33 34 import com.android.documentsui.model.DocumentInfo; 35 36 /** 37 * Display document title editor and save button. 38 */ 39 public class SaveFragment extends Fragment { 40 public static final String TAG = "SaveFragment"; 41 42 private DocumentInfo mReplaceTarget; 43 private EditText mDisplayName; 44 private Button mSave; 45 private ProgressBar mProgress; 46 private boolean mIgnoreNextEdit; 47 48 private static final String EXTRA_MIME_TYPE = "mime_type"; 49 private static final String EXTRA_DISPLAY_NAME = "display_name"; 50 show(FragmentManager fm, String mimeType, String displayName)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(); 60 ft.replace(R.id.container_save, fragment, TAG); 61 ft.commitAllowingStateLoss(); 62 } 63 get(FragmentManager fm)64 public static SaveFragment get(FragmentManager fm) { 65 return (SaveFragment) fm.findFragmentByTag(TAG); 66 } 67 68 @Override onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)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); 76 icon.setImageDrawable( 77 IconUtils.loadMimeIcon(context, getArguments().getString(EXTRA_MIME_TYPE))); 78 79 mDisplayName = (EditText) view.findViewById(android.R.id.title); 80 mDisplayName.addTextChangedListener(mDisplayNameWatcher); 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 87 mProgress = (ProgressBar) view.findViewById(android.R.id.progress); 88 89 return view; 90 } 91 92 private TextWatcher mDisplayNameWatcher = new TextWatcher() { 93 @Override 94 public void onTextChanged(CharSequence s, int start, int before, int count) { 95 if (mIgnoreNextEdit) { 96 mIgnoreNextEdit = false; 97 } else { 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 110 } 111 }; 112 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 */ setReplaceTarget(DocumentInfo replaceTarget)132 public void setReplaceTarget(DocumentInfo replaceTarget) { 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 } 140 } 141 setSaveEnabled(boolean enabled)142 public void setSaveEnabled(boolean enabled) { 143 mSave.setEnabled(enabled); 144 } 145 setPending(boolean pending)146 public void setPending(boolean pending) { 147 mSave.setVisibility(pending ? View.INVISIBLE : View.VISIBLE); 148 mProgress.setVisibility(pending ? View.VISIBLE : View.GONE); 149 } 150 } 151