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
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.text.Editable;
25import android.text.TextWatcher;
26import android.util.Log;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.Button;
31import android.widget.EditText;
32import android.widget.ImageView;
33
34import com.android.documentsui.model.DocumentInfo;
35
36/**
37 * Display document title editor and save button.
38 */
39public 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 boolean mIgnoreNextEdit;
46
47    private static final String EXTRA_MIME_TYPE = "mime_type";
48    private static final String EXTRA_DISPLAY_NAME = "display_name";
49
50    public static void show(FragmentManager fm, String mimeType, String displayName) {
51        final Bundle args = new Bundle();
52        args.putString(EXTRA_MIME_TYPE, mimeType);
53        args.putString(EXTRA_DISPLAY_NAME, displayName);
54
55        final SaveFragment fragment = new SaveFragment();
56        fragment.setArguments(args);
57
58        final FragmentTransaction ft = fm.beginTransaction();
59        ft.replace(R.id.container_save, fragment, TAG);
60        ft.commitAllowingStateLoss();
61    }
62
63    public static SaveFragment get(FragmentManager fm) {
64        return (SaveFragment) fm.findFragmentByTag(TAG);
65    }
66
67    @Override
68    public View onCreateView(
69            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
70        final Context context = inflater.getContext();
71
72        final View view = inflater.inflate(R.layout.fragment_save, container, false);
73
74        final ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
75        icon.setImageDrawable(
76                IconUtils.loadMimeIcon(context, getArguments().getString(EXTRA_MIME_TYPE)));
77
78        mDisplayName = (EditText) view.findViewById(android.R.id.title);
79        mDisplayName.addTextChangedListener(mDisplayNameWatcher);
80        mDisplayName.setText(getArguments().getString(EXTRA_DISPLAY_NAME));
81
82        mSave = (Button) view.findViewById(android.R.id.button1);
83        mSave.setOnClickListener(mSaveListener);
84        mSave.setEnabled(false);
85
86        return view;
87    }
88
89    private TextWatcher mDisplayNameWatcher = new TextWatcher() {
90        @Override
91        public void onTextChanged(CharSequence s, int start, int before, int count) {
92            if (mIgnoreNextEdit) {
93                mIgnoreNextEdit = false;
94            } else {
95                Log.d(TAG, "onTextChanged!");
96                mReplaceTarget = null;
97            }
98        }
99
100        @Override
101        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
102            // ignored
103        }
104
105        @Override
106        public void afterTextChanged(Editable s) {
107            // ignored
108        }
109    };
110
111    private View.OnClickListener mSaveListener = new View.OnClickListener() {
112        @Override
113        public void onClick(View v) {
114            final DocumentsActivity activity = DocumentsActivity.get(SaveFragment.this);
115            if (mReplaceTarget != null) {
116                activity.onSaveRequested(mReplaceTarget);
117            } else {
118                final String mimeType = getArguments().getString(EXTRA_MIME_TYPE);
119                final String displayName = mDisplayName.getText().toString();
120                activity.onSaveRequested(mimeType, displayName);
121            }
122        }
123    };
124
125    /**
126     * Set given document as target for in-place writing if user hits save
127     * without changing the filename. Can be set to {@code null} if user
128     * navigates outside the target directory.
129     */
130    public void setReplaceTarget(DocumentInfo replaceTarget) {
131        mReplaceTarget = replaceTarget;
132
133        if (mReplaceTarget != null) {
134            getArguments().putString(EXTRA_DISPLAY_NAME, replaceTarget.displayName);
135            mIgnoreNextEdit = true;
136            mDisplayName.setText(replaceTarget.displayName);
137        }
138    }
139
140    public void setSaveEnabled(boolean enabled) {
141        mSave.setEnabled(enabled);
142    }
143}
144