aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/research/FeedbackFragment.java
blob: 75fbbf1ba1e370fb87a97876b3c5015eb19f3a80 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.inputmethod.research;

import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.android.inputmethod.latin.R;

public class FeedbackFragment extends Fragment implements OnClickListener {
    private static final String TAG = FeedbackFragment.class.getSimpleName();

    public static final String KEY_FEEDBACK_STRING = "FeedbackString";
    public static final String KEY_INCLUDE_ACCOUNT_NAME = "IncludeAccountName";
    public static final String KEY_HAS_USER_RECORDING = "HasRecording";

    private EditText mEditText;
    private CheckBox mIncludingAccountNameCheckBox;
    private CheckBox mIncludingUserRecordingCheckBox;
    private Button mSendButton;
    private Button mCancelButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.research_feedback_fragment_layout, container,
                false);
        mEditText = (EditText) view.findViewById(R.id.research_feedback_contents);
        mEditText.requestFocus();
        mIncludingAccountNameCheckBox = (CheckBox) view.findViewById(
                R.id.research_feedback_include_account_name);
        mIncludingUserRecordingCheckBox = (CheckBox) view.findViewById(
                R.id.research_feedback_include_recording_checkbox);
        mIncludingUserRecordingCheckBox.setOnClickListener(this);

        mSendButton = (Button) view.findViewById(R.id.research_feedback_send_button);
        mSendButton.setOnClickListener(this);
        mCancelButton = (Button) view.findViewById(R.id.research_feedback_cancel_button);
        mCancelButton.setOnClickListener(this);

        if (savedInstanceState != null) {
            restoreState(savedInstanceState);
        } else {
            final Bundle bundle = getActivity().getIntent().getExtras();
            if (bundle != null) {
                restoreState(bundle);
            }
        }
        return view;
    }

    @Override
    public void onClick(final View view) {
        final ResearchLogger researchLogger = ResearchLogger.getInstance();
        if (view == mIncludingUserRecordingCheckBox) {
            if (mIncludingUserRecordingCheckBox.isChecked()) {
                final Bundle bundle = new Bundle();
                onSaveInstanceState(bundle);

                // Let the user make a recording
                getActivity().finish();

                researchLogger.setFeedbackDialogBundle(bundle);
                researchLogger.onLeavingSendFeedbackDialog();
                researchLogger.startRecording();
            }
        } else if (view == mSendButton) {
            final Editable editable = mEditText.getText();
            final String feedbackContents = editable.toString();
            if (TextUtils.isEmpty(feedbackContents)) {
                Toast.makeText(getActivity(),
                        R.string.research_feedback_empty_feedback_error_message,
                        Toast.LENGTH_LONG).show();
            } else {
                final boolean isIncludingAccountName = mIncludingAccountNameCheckBox.isChecked();
                researchLogger.sendFeedback(feedbackContents, false /* isIncludingHistory */,
                        isIncludingAccountName, mIncludingUserRecordingCheckBox.isChecked());
                getActivity().finish();
                researchLogger.setFeedbackDialogBundle(null);
                researchLogger.onLeavingSendFeedbackDialog();
            }
        } else if (view == mCancelButton) {
            Log.d(TAG, "Finishing");
            getActivity().finish();
            researchLogger.setFeedbackDialogBundle(null);
            researchLogger.onLeavingSendFeedbackDialog();
        } else {
            Log.e(TAG, "Unknown view passed to FeedbackFragment.onClick()");
        }
    }

    @Override
    public void onSaveInstanceState(final Bundle bundle) {
        final String savedFeedbackString = mEditText.getText().toString();

        bundle.putString(KEY_FEEDBACK_STRING, savedFeedbackString);
        bundle.putBoolean(KEY_INCLUDE_ACCOUNT_NAME, mIncludingAccountNameCheckBox.isChecked());
        bundle.putBoolean(KEY_HAS_USER_RECORDING, mIncludingUserRecordingCheckBox.isChecked());
    }

    private void restoreState(final Bundle bundle) {
        mEditText.setText(bundle.getString(KEY_FEEDBACK_STRING));
        mIncludingAccountNameCheckBox.setChecked(bundle.getBoolean(KEY_INCLUDE_ACCOUNT_NAME));
        mIncludingUserRecordingCheckBox.setChecked(bundle.getBoolean(KEY_HAS_USER_RECORDING));
    }
}