1/*
2 * Copyright (C) 2017 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.deviceinfo;
17
18import android.app.Fragment;
19import android.content.Context;
20import android.os.Build;
21import android.support.v7.preference.Preference;
22import android.support.v7.preference.PreferenceScreen;
23import android.text.TextUtils;
24
25import com.android.settings.core.PreferenceControllerMixin;
26import com.android.settingslib.DeviceInfoUtils;
27import com.android.settingslib.core.AbstractPreferenceController;
28
29public class DeviceModelPreferenceController extends AbstractPreferenceController implements
30        PreferenceControllerMixin {
31
32    private static final String KEY_DEVICE_MODEL = "device_model";
33
34    private final Fragment mHost;
35
36    public DeviceModelPreferenceController(Context context, Fragment host) {
37        super(context);
38        mHost = host;
39    }
40
41    @Override
42    public boolean isAvailable() {
43        return true;
44    }
45
46    @Override
47    public void displayPreference(PreferenceScreen screen) {
48        super.displayPreference(screen);
49        final Preference pref = screen.findPreference(KEY_DEVICE_MODEL);
50        if (pref != null) {
51            pref.setSummary(getDeviceModel());
52        }
53    }
54
55    @Override
56    public String getPreferenceKey() {
57        return KEY_DEVICE_MODEL;
58    }
59
60    @Override
61    public boolean handlePreferenceTreeClick(Preference preference) {
62        if (!TextUtils.equals(preference.getKey(), KEY_DEVICE_MODEL)) {
63            return false;
64        }
65        final HardwareInfoDialogFragment fragment = HardwareInfoDialogFragment.newInstance();
66        fragment.show(mHost.getFragmentManager(), HardwareInfoDialogFragment.TAG);
67        return true;
68    }
69
70    public static String getDeviceModel() {
71        return Build.MODEL + DeviceInfoUtils.getMsvSuffix();
72    }
73}
74