1/*
2 * Copyright (C) 2016 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.accounts;
17
18import android.content.Context;
19import android.content.Intent;
20import android.content.pm.ResolveInfo;
21import android.content.pm.UserInfo;
22import android.content.res.Resources;
23import android.os.UserHandle;
24import android.os.UserManager;
25import android.support.v7.preference.Preference;
26
27import com.android.settings.R;
28import com.android.settings.core.PreferenceControllerMixin;
29import com.android.settings.search.SearchIndexableRaw;
30import com.android.settingslib.core.AbstractPreferenceController;
31
32import java.util.List;
33
34public class EmergencyInfoPreferenceController extends AbstractPreferenceController
35        implements PreferenceControllerMixin {
36
37    private static final String KEY_EMERGENCY_INFO = "emergency_info";
38    private static final String ACTION_EDIT_EMERGENCY_INFO = "android.settings.EDIT_EMERGENCY_INFO";
39    private static final String PACKAGE_NAME_EMERGENCY = "com.android.emergency";
40
41    public EmergencyInfoPreferenceController(Context context) {
42        super(context);
43    }
44
45    @Override
46    public void updateRawDataToIndex(List<SearchIndexableRaw> rawData) {
47        if (isAvailable()) {
48            SearchIndexableRaw data = new SearchIndexableRaw(mContext);
49            final Resources res = mContext.getResources();
50            data.title = res.getString(com.android.settings.R.string.emergency_info_title);
51            data.screenTitle = res.getString(com.android.settings.R.string.emergency_info_title);
52            rawData.add(data);
53        }
54    }
55
56    public void updateState(Preference preference) {
57        UserInfo info = mContext.getSystemService(UserManager.class).getUserInfo(
58            UserHandle.myUserId());
59        preference.setSummary(mContext.getString(R.string.emergency_info_summary, info.name));
60    }
61
62    @Override
63    public boolean handlePreferenceTreeClick(Preference preference) {
64        if (KEY_EMERGENCY_INFO.equals(preference.getKey())) {
65            Intent intent = new Intent(ACTION_EDIT_EMERGENCY_INFO);
66            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
67            mContext.startActivity(intent);
68            return true;
69        }
70        return false;
71    }
72
73    @Override
74    public boolean isAvailable() {
75        Intent intent = new Intent(ACTION_EDIT_EMERGENCY_INFO).setPackage(PACKAGE_NAME_EMERGENCY);
76        List<ResolveInfo> infos = mContext.getPackageManager().queryIntentActivities(intent, 0);
77        return infos != null && !infos.isEmpty();
78    }
79
80    @Override
81    public String getPreferenceKey() {
82        return KEY_EMERGENCY_INFO;
83    }
84}
85