1/*
2 * Copyright (C) 2013 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.settings.location;
18
19import android.content.Intent;
20import android.os.UserHandle;
21import android.text.TextUtils;
22import android.util.Log;
23
24import com.android.internal.annotations.Immutable;
25import com.android.internal.util.Preconditions;
26
27/**
28 * Specifies a setting that is being injected into Settings > Location > Location services.
29 *
30 * @see android.location.SettingInjectorService
31 */
32@Immutable
33class InjectedSetting {
34
35    /**
36     * Package for the subclass of {@link android.location.SettingInjectorService} and for the
37     * settings activity.
38     */
39    public final String packageName;
40
41    /**
42     * Class name for the subclass of {@link android.location.SettingInjectorService} that
43     * specifies dynamic values for the location setting.
44     */
45    public final String className;
46
47    /**
48     * The {@link android.support.v7.preference.Preference#getTitle()} value.
49     */
50    public final String title;
51
52    /**
53     * The {@link android.support.v7.preference.Preference#getIcon()} value.
54     */
55    public final int iconId;
56
57    /**
58     * The user/profile associated with this setting (e.g. managed profile)
59     */
60    public final UserHandle mUserHandle;
61
62    /**
63     * The activity to launch to allow the user to modify the settings value. Assumed to be in the
64     * {@link #packageName} package.
65     */
66    public final String settingsActivity;
67
68    private InjectedSetting(String packageName, String className,
69            String title, int iconId, UserHandle userHandle, String settingsActivity) {
70        this.packageName = Preconditions.checkNotNull(packageName, "packageName");
71        this.className = Preconditions.checkNotNull(className, "className");
72        this.title = Preconditions.checkNotNull(title, "title");
73        this.iconId = iconId;
74        this.mUserHandle = userHandle;
75        this.settingsActivity = Preconditions.checkNotNull(settingsActivity);
76    }
77
78    /**
79     * Returns a new instance, or null.
80     */
81    public static InjectedSetting newInstance(String packageName, String className,
82            String title, int iconId, UserHandle userHandle, String settingsActivity) {
83        if (packageName == null || className == null ||
84                TextUtils.isEmpty(title) || TextUtils.isEmpty(settingsActivity)) {
85            if (Log.isLoggable(SettingsInjector.TAG, Log.WARN)) {
86                Log.w(SettingsInjector.TAG, "Illegal setting specification: package="
87                        + packageName + ", class=" + className
88                        + ", title=" + title + ", settingsActivity=" + settingsActivity);
89            }
90            return null;
91        }
92        return new InjectedSetting(packageName, className, title, iconId, userHandle,
93                settingsActivity);
94    }
95
96    @Override
97    public String toString() {
98        return "InjectedSetting{" +
99                "mPackageName='" + packageName + '\'' +
100                ", mClassName='" + className + '\'' +
101                ", label=" + title +
102                ", iconId=" + iconId +
103                ", userId=" + mUserHandle.getIdentifier() +
104                ", settingsActivity='" + settingsActivity + '\'' +
105                '}';
106    }
107
108    /**
109     * Returns the intent to start the {@link #className} service.
110     */
111    public Intent getServiceIntent() {
112        Intent intent = new Intent();
113        intent.setClassName(packageName, className);
114        return intent;
115    }
116
117    @Override
118    public boolean equals(Object o) {
119        if (this == o) return true;
120        if (!(o instanceof InjectedSetting)) return false;
121
122        InjectedSetting that = (InjectedSetting) o;
123
124        return packageName.equals(that.packageName) && className.equals(that.className)
125                && title.equals(that.title) && iconId == that.iconId
126                && mUserHandle.equals(that.mUserHandle)
127                && settingsActivity.equals(that.settingsActivity);
128    }
129
130    @Override
131    public int hashCode() {
132        int result = packageName.hashCode();
133        result = 31 * result + className.hashCode();
134        result = 31 * result + title.hashCode();
135        result = 31 * result + iconId;
136        result = 31 * result + mUserHandle.hashCode();
137        result = 31 * result + settingsActivity.hashCode();
138        return result;
139    }
140}
141