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; |
| 24 | import android.view.LayoutInflater; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
| 27 | import android.widget.Button; |
| 28 | import android.widget.EditText; |
| 29 | import android.widget.ImageView; |
| 30 | |
| 31 | /** |
| 32 | * Display document title editor and save button. |
| 33 | */ |
| 34 | public class SaveFragment extends Fragment { |
| 35 | public static final String TAG = "SaveFragment"; |
| 36 | |
| 37 | private EditText mDisplayName; |
| 38 | private Button mSave; |
| 39 | |
| 40 | private static final String EXTRA_MIME_TYPE = "mime_type"; |
| 41 | private static final String EXTRA_DISPLAY_NAME = "display_name"; |
| 42 | |
| 43 | public static void show(FragmentManager fm, String mimeType, String displayName) { |
| 44 | final Bundle args = new Bundle(); |
| 45 | args.putString(EXTRA_MIME_TYPE, mimeType); |
| 46 | args.putString(EXTRA_DISPLAY_NAME, displayName); |
| 47 | |
| 48 | final SaveFragment fragment = new SaveFragment(); |
| 49 | fragment.setArguments(args); |
| 50 | |
| 51 | final FragmentTransaction ft = fm.beginTransaction(); |
| 52 | ft.replace(R.id.save, fragment, TAG); |
| 53 | ft.commitAllowingStateLoss(); |
| 54 | } |
| 55 | |
| 56 | public static SaveFragment get(FragmentManager fm) { |
| 57 | return (SaveFragment) fm.findFragmentByTag(TAG); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public View onCreateView( |
| 62 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 63 | final Context context = inflater.getContext(); |
| 64 | |
| 65 | final View view = inflater.inflate(R.layout.fragment_save, container, false); |
| 66 | |
| 67 | final ImageView icon = (ImageView) view.findViewById(android.R.id.icon); |
| 68 | icon.setImageDrawable(DocumentsActivity.resolveDocumentIcon( |
| 69 | context, getArguments().getString(EXTRA_MIME_TYPE))); |
| 70 | |
| 71 | mDisplayName = (EditText) view.findViewById(android.R.id.title); |
| 72 | mDisplayName.setText(getArguments().getString(EXTRA_DISPLAY_NAME)); |
| 73 | |
| 74 | mSave = (Button) view.findViewById(android.R.id.button1); |
| 75 | mSave.setOnClickListener(mSaveListener); |
| 76 | mSave.setEnabled(false); |
| 77 | |
| 78 | return view; |
| 79 | } |
| 80 | |
| 81 | private View.OnClickListener mSaveListener = new View.OnClickListener() { |
| 82 | @Override |
| 83 | public void onClick(View v) { |
| 84 | final String mimeType = getArguments().getString(EXTRA_MIME_TYPE); |
| 85 | final String displayName = getArguments().getString(EXTRA_DISPLAY_NAME); |
| 86 | ((DocumentsActivity) getActivity()).onSaveRequested(mimeType, displayName); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | public void setDisplayName(String displayName) { |
| 91 | getArguments().putString(EXTRA_DISPLAY_NAME, displayName); |
| 92 | mDisplayName.setText(displayName); |
| 93 | } |
| 94 | |
| 95 | public void setSaveEnabled(boolean enabled) { |
| 96 | mSave.setEnabled(enabled); |
| 97 | } |
| 98 | } |