1/*
2 * Copyright (C) 2014 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 */
16package com.android.settings;
17
18import java.util.ArrayList;
19import java.util.List;
20
21import android.content.Context;
22import android.content.res.Resources;
23import android.os.Bundle;
24import android.provider.Settings;
25import android.provider.Settings.SettingNotFoundException;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.Switch;
30
31import com.android.settings.search.BaseSearchIndexProvider;
32import com.android.settings.search.Indexable;
33import com.android.settings.search.SearchIndexableRaw;
34import com.android.settings.widget.SwitchBar;
35
36/**
37 * Screen pinning settings.
38 */
39public class ScreenPinningSettings extends SettingsPreferenceFragment
40        implements SwitchBar.OnSwitchChangeListener, Indexable {
41
42    private SwitchBar mSwitchBar;
43
44    @Override
45    public void onActivityCreated(Bundle savedInstanceState) {
46        super.onActivityCreated(savedInstanceState);
47
48        final SettingsActivity activity = (SettingsActivity) getActivity();
49
50        mSwitchBar = activity.getSwitchBar();
51        mSwitchBar.addOnSwitchChangeListener(this);
52        mSwitchBar.show();
53        mSwitchBar.setChecked(isLockToAppEnabled());
54    }
55
56    @Override
57    public void onDestroyView() {
58        super.onDestroyView();
59
60        mSwitchBar.removeOnSwitchChangeListener(this);
61        mSwitchBar.hide();
62    }
63
64    @Override
65    public View onCreateView(LayoutInflater inflater, ViewGroup container,
66            Bundle savedInstanceState) {
67        return inflater.inflate(R.layout.screen_pinning_instructions, null);
68    }
69
70    private boolean isLockToAppEnabled() {
71        try {
72            return Settings.System.getInt(getContentResolver(), Settings.System.LOCK_TO_APP_ENABLED)
73                != 0;
74        } catch (SettingNotFoundException e) {
75            return false;
76        }
77    }
78
79    private void setLockToAppEnabled(boolean isEnabled) {
80        Settings.System.putInt(getContentResolver(), Settings.System.LOCK_TO_APP_ENABLED,
81                isEnabled ? 1 : 0);
82    }
83
84    /**
85     * Listens to the state change of the lock-to-app master switch.
86     */
87    @Override
88    public void onSwitchChanged(Switch switchView, boolean isChecked) {
89        setLockToAppEnabled(isChecked);
90    }
91
92    /**
93     * For search
94     */
95    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
96        new BaseSearchIndexProvider() {
97            @Override
98            public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
99                final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
100
101                final Resources res = context.getResources();
102
103                // Add fragment title
104                SearchIndexableRaw data = new SearchIndexableRaw(context);
105                data.title = res.getString(R.string.screen_pinning_title);
106                data.screenTitle = res.getString(R.string.screen_pinning_title);
107                result.add(data);
108
109                // Screen pinning description.
110                data = new SearchIndexableRaw(context);
111                data.title = res.getString(R.string.screen_pinning_description);
112                data.screenTitle = res.getString(R.string.screen_pinning_title);
113                result.add(data);
114
115                return result;
116            }
117        };
118}
119