1/*
2 * Copyright (C) 2008 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.latin.settings;
18
19import android.app.Activity;
20import android.content.Intent;
21import android.os.Build;
22import android.os.Bundle;
23import android.preference.Preference;
24import android.preference.PreferenceScreen;
25import android.provider.Settings.Secure;
26import android.view.Menu;
27import android.view.MenuInflater;
28import android.view.MenuItem;
29
30import com.android.inputmethod.latin.R;
31import com.android.inputmethod.latin.define.ProductionFlags;
32import com.android.inputmethod.latin.utils.ApplicationUtils;
33import com.android.inputmethod.latin.utils.FeedbackUtils;
34import com.android.inputmethodcommon.InputMethodSettingsFragment;
35
36public final class SettingsFragment extends InputMethodSettingsFragment {
37    // We don't care about menu grouping.
38    private static final int NO_MENU_GROUP = Menu.NONE;
39    // The first menu item id and order.
40    private static final int MENU_ABOUT = Menu.FIRST;
41    // The second menu item id and order.
42    private static final int MENU_HELP_AND_FEEDBACK = Menu.FIRST + 1;
43
44    @Override
45    public void onCreate(final Bundle icicle) {
46        super.onCreate(icicle);
47        setHasOptionsMenu(true);
48        setInputMethodSettingsCategoryTitle(R.string.language_selection_title);
49        setSubtypeEnablerTitle(R.string.select_language);
50        addPreferencesFromResource(R.xml.prefs);
51        final PreferenceScreen preferenceScreen = getPreferenceScreen();
52        preferenceScreen.setTitle(
53                ApplicationUtils.getActivityTitleResId(getActivity(), SettingsActivity.class));
54        if (!ProductionFlags.ENABLE_ACCOUNT_SIGN_IN) {
55            final Preference accountsPreference = findPreference(Settings.SCREEN_ACCOUNTS);
56            preferenceScreen.removePreference(accountsPreference);
57        }
58    }
59
60    @Override
61    public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
62        if (FeedbackUtils.isHelpAndFeedbackFormSupported()) {
63            menu.add(NO_MENU_GROUP, MENU_HELP_AND_FEEDBACK /* itemId */,
64                    MENU_HELP_AND_FEEDBACK /* order */, R.string.help_and_feedback);
65        }
66        final int aboutResId = FeedbackUtils.getAboutKeyboardTitleResId();
67        if (aboutResId != 0) {
68            menu.add(NO_MENU_GROUP, MENU_ABOUT /* itemId */, MENU_ABOUT /* order */, aboutResId);
69        }
70    }
71
72    @Override
73    public boolean onOptionsItemSelected(final MenuItem item) {
74        final Activity activity = getActivity();
75        if (!isUserSetupComplete(activity)) {
76            // If setup is not complete, it's not safe to launch Help or other activities
77            // because they might go to the Play Store.  See b/19866981.
78            return true;
79        }
80        final int itemId = item.getItemId();
81        if (itemId == MENU_HELP_AND_FEEDBACK) {
82            FeedbackUtils.showHelpAndFeedbackForm(activity);
83            return true;
84        }
85        if (itemId == MENU_ABOUT) {
86            final Intent aboutIntent = FeedbackUtils.getAboutKeyboardIntent(activity);
87            if (aboutIntent != null) {
88                startActivity(aboutIntent);
89                return true;
90            }
91        }
92        return super.onOptionsItemSelected(item);
93    }
94
95    private static boolean isUserSetupComplete(final Activity activity) {
96        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
97            return true;
98        }
99        return Secure.getInt(activity.getContentResolver(), "user_setup_complete", 0) != 0;
100    }
101}
102