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.util.Log; 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; 33 import android.widget.ProgressBar; 34 35 import com.android.documentsui.model.DocumentInfo; 36 37 /** 38 * Display document title editor and save button. 39 */ 40 public class SaveFragment extends Fragment { 41 public static final String TAG = "SaveFragment"; 42 43 private DocumentInfo mReplaceTarget; 44 private EditText mDisplayName; 45 private Button mSave; 46 private ProgressBar mProgress; 47 private boolean mIgnoreNextEdit; 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(); 61 ft.replace(R.id.container_save, fragment, TAG); 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); 77 icon.setImageDrawable( 78 IconUtils.loadMimeIcon(context, getArguments().getString(EXTRA_MIME_TYPE))); 79 80 mDisplayName = (EditText) view.findViewById(android.R.id.title); 81 mDisplayName.addTextChangedListener(mDisplayNameWatcher); 82 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 88 mProgress = (ProgressBar) view.findViewById(android.R.id.progress); 89 90 return view; 91 } 92 93 private TextWatcher mDisplayNameWatcher = new TextWatcher() { 94 @Override 95 public void onTextChanged(CharSequence s, int start, int before, int count) { 96 if (mIgnoreNextEdit) { 97 mIgnoreNextEdit = false; 98 } else { 99 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 111 } 112 }; 113 114 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 */ 133 public void setReplaceTarget(DocumentInfo replaceTarget) { 134 mReplaceTarget = replaceTarget; 135 136 if (mReplaceTarget != null) { 137 getArguments().putString(EXTRA_DISPLAY_NAME, replaceTarget.displayName); 138 mIgnoreNextEdit = true; 139 mDisplayName.setText(replaceTarget.displayName); 140 } 141 } 142 143 public void setSaveEnabled(boolean enabled) { 144 mSave.setEnabled(enabled); 145 } 146 147 public void setPending(boolean pending) { 148 mSave.setVisibility(pending ? View.INVISIBLE : View.VISIBLE); 149 mProgress.setVisibility(pending ? View.VISIBLE : View.GONE); 150 } 151 } 152