1/*
2 * Copyright (C) 2012 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.inputmethod.research;
18
19import android.app.Activity;
20import android.app.Fragment;
21import android.os.Bundle;
22import android.text.Editable;
23import android.text.TextUtils;
24import android.util.Log;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.View.OnClickListener;
28import android.view.ViewGroup;
29import android.widget.Button;
30import android.widget.CheckBox;
31import android.widget.EditText;
32import android.widget.Toast;
33
34import com.android.inputmethod.latin.R;
35
36public class FeedbackFragment extends Fragment implements OnClickListener {
37    private static final String TAG = FeedbackFragment.class.getSimpleName();
38
39    public static final String KEY_FEEDBACK_STRING = "FeedbackString";
40    public static final String KEY_INCLUDE_ACCOUNT_NAME = "IncludeAccountName";
41    public static final String KEY_HAS_USER_RECORDING = "HasRecording";
42
43    private EditText mEditText;
44    private CheckBox mIncludingAccountNameCheckBox;
45    private CheckBox mIncludingUserRecordingCheckBox;
46    private Button mSendButton;
47    private Button mCancelButton;
48
49    @Override
50    public View onCreateView(LayoutInflater inflater, ViewGroup container,
51            Bundle savedInstanceState) {
52        final View view = inflater.inflate(R.layout.research_feedback_fragment_layout, container,
53                false);
54        mEditText = (EditText) view.findViewById(R.id.research_feedback_contents);
55        mEditText.requestFocus();
56        mIncludingAccountNameCheckBox = (CheckBox) view.findViewById(
57                R.id.research_feedback_include_account_name);
58        mIncludingUserRecordingCheckBox = (CheckBox) view.findViewById(
59                R.id.research_feedback_include_recording_checkbox);
60        mIncludingUserRecordingCheckBox.setOnClickListener(this);
61
62        mSendButton = (Button) view.findViewById(R.id.research_feedback_send_button);
63        mSendButton.setOnClickListener(this);
64        mCancelButton = (Button) view.findViewById(R.id.research_feedback_cancel_button);
65        mCancelButton.setOnClickListener(this);
66
67        if (savedInstanceState != null) {
68            restoreState(savedInstanceState);
69        } else {
70            final Bundle bundle = getActivity().getIntent().getExtras();
71            if (bundle != null) {
72                restoreState(bundle);
73            }
74        }
75        return view;
76    }
77
78    @Override
79    public void onClick(final View view) {
80        final ResearchLogger researchLogger = ResearchLogger.getInstance();
81        if (view == mIncludingUserRecordingCheckBox) {
82            if (mIncludingUserRecordingCheckBox.isChecked()) {
83                final Bundle bundle = new Bundle();
84                onSaveInstanceState(bundle);
85
86                // Let the user make a recording
87                getActivity().finish();
88
89                researchLogger.setFeedbackDialogBundle(bundle);
90                researchLogger.onLeavingSendFeedbackDialog();
91                researchLogger.startRecording();
92            }
93        } else if (view == mSendButton) {
94            final Editable editable = mEditText.getText();
95            final String feedbackContents = editable.toString();
96            if (TextUtils.isEmpty(feedbackContents)) {
97                Toast.makeText(getActivity(),
98                        R.string.research_feedback_empty_feedback_error_message,
99                        Toast.LENGTH_LONG).show();
100            } else {
101                final boolean isIncludingAccountName = mIncludingAccountNameCheckBox.isChecked();
102                researchLogger.sendFeedback(feedbackContents, false /* isIncludingHistory */,
103                        isIncludingAccountName, mIncludingUserRecordingCheckBox.isChecked());
104                getActivity().finish();
105                researchLogger.setFeedbackDialogBundle(null);
106                researchLogger.onLeavingSendFeedbackDialog();
107            }
108        } else if (view == mCancelButton) {
109            Log.d(TAG, "Finishing");
110            getActivity().finish();
111            researchLogger.setFeedbackDialogBundle(null);
112            researchLogger.onLeavingSendFeedbackDialog();
113        } else {
114            Log.e(TAG, "Unknown view passed to FeedbackFragment.onClick()");
115        }
116    }
117
118    @Override
119    public void onSaveInstanceState(final Bundle bundle) {
120        final String savedFeedbackString = mEditText.getText().toString();
121
122        bundle.putString(KEY_FEEDBACK_STRING, savedFeedbackString);
123        bundle.putBoolean(KEY_INCLUDE_ACCOUNT_NAME, mIncludingAccountNameCheckBox.isChecked());
124        bundle.putBoolean(KEY_HAS_USER_RECORDING, mIncludingUserRecordingCheckBox.isChecked());
125    }
126
127    private void restoreState(final Bundle bundle) {
128        mEditText.setText(bundle.getString(KEY_FEEDBACK_STRING));
129        mIncludingAccountNameCheckBox.setChecked(bundle.getBoolean(KEY_INCLUDE_ACCOUNT_NAME));
130        mIncludingUserRecordingCheckBox.setChecked(bundle.getBoolean(KEY_HAS_USER_RECORDING));
131    }
132}
133