blob: 19a1d2a45baa040945cf9eb15dad7a30e88f442a [file] [log] [blame]
Jeff Sharkey54e55b72013-06-30 20:02:59 -07001/*
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
17package com.android.documentsui;
18
19import android.app.Fragment;
20import android.app.FragmentManager;
21import android.app.FragmentTransaction;
22import android.content.Context;
23import android.os.Bundle;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.Button;
28import android.widget.EditText;
29import android.widget.ImageView;
30
31/**
32 * Display document title editor and save button.
33 */
34public 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}