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