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