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