Home | History | Annotate | Download | only in location
      1 /*
      2  * Copyright (C) 2018 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 
     17 package com.android.settingslib.location;
     18 
     19 import android.content.Intent;
     20 import android.os.UserHandle;
     21 import android.text.TextUtils;
     22 import android.util.Log;
     23 
     24 import com.android.internal.annotations.Immutable;
     25 
     26 import java.util.Objects;
     27 
     28 /**
     29  * Specifies a setting that is being injected into Settings > Location > Location services.
     30  *
     31  * @see android.location.SettingInjectorService
     32  */
     33 @Immutable
     34 public class InjectedSetting {
     35 
     36     /**
     37      * Package for the subclass of {@link android.location.SettingInjectorService} and for the
     38      * settings activity.
     39      */
     40     public final String packageName;
     41 
     42     /**
     43      * Class name for the subclass of {@link android.location.SettingInjectorService} that
     44      * specifies dynamic values for the location setting.
     45      */
     46     public final String className;
     47 
     48     /**
     49      * The {@link android.support.v7.preference.Preference#getTitle()} value.
     50      */
     51     public final String title;
     52 
     53     /**
     54      * The {@link android.support.v7.preference.Preference#getIcon()} value.
     55      */
     56     public final int iconId;
     57 
     58     /**
     59      * The user/profile associated with this setting (e.g. managed profile)
     60      */
     61     public final UserHandle mUserHandle;
     62 
     63     /**
     64      * The activity to launch to allow the user to modify the settings value. Assumed to be in the
     65      * {@link #packageName} package.
     66      */
     67     public final String settingsActivity;
     68 
     69     /**
     70      * The user restriction associated with this setting.
     71      */
     72     public final String userRestriction;
     73 
     74     private InjectedSetting(Builder builder) {
     75         this.packageName = builder.mPackageName;
     76         this.className = builder.mClassName;
     77         this.title = builder.mTitle;
     78         this.iconId = builder.mIconId;
     79         this.mUserHandle = builder.mUserHandle;
     80         this.settingsActivity = builder.mSettingsActivity;
     81         this.userRestriction = builder.mUserRestriction;
     82     }
     83 
     84     @Override
     85     public String toString() {
     86         return "InjectedSetting{" +
     87                 "mPackageName='" + packageName + '\'' +
     88                 ", mClassName='" + className + '\'' +
     89                 ", label=" + title +
     90                 ", iconId=" + iconId +
     91                 ", userId=" + mUserHandle.getIdentifier() +
     92                 ", settingsActivity='" + settingsActivity + '\'' +
     93                 ", userRestriction='" + userRestriction +
     94                 '}';
     95     }
     96 
     97     /**
     98      * Returns the intent to start the {@link #className} service.
     99      */
    100     public Intent getServiceIntent() {
    101         Intent intent = new Intent();
    102         intent.setClassName(packageName, className);
    103         return intent;
    104     }
    105 
    106     @Override
    107     public boolean equals(Object o) {
    108         if (this == o) return true;
    109         if (!(o instanceof InjectedSetting)) return false;
    110 
    111         InjectedSetting that = (InjectedSetting) o;
    112 
    113         return Objects.equals(packageName, that.packageName)
    114                 && Objects.equals(className, that.className)
    115                 && Objects.equals(title, that.title)
    116                 && Objects.equals(iconId, that.iconId)
    117                 && Objects.equals(mUserHandle, that.mUserHandle)
    118                 && Objects.equals(settingsActivity, that.settingsActivity)
    119                 && Objects.equals(userRestriction, that.userRestriction);
    120     }
    121 
    122     @Override
    123     public int hashCode() {
    124         int result = packageName.hashCode();
    125         result = 31 * result + className.hashCode();
    126         result = 31 * result + title.hashCode();
    127         result = 31 * result + iconId;
    128         result = 31 * result + (mUserHandle == null ? 0 : mUserHandle.hashCode());
    129         result = 31 * result + settingsActivity.hashCode();
    130         result = 31 * result + (userRestriction == null ? 0 : userRestriction.hashCode());
    131         return result;
    132     }
    133 
    134     public static class Builder {
    135         private String mPackageName;
    136         private String mClassName;
    137         private String mTitle;
    138         private int mIconId;
    139         private UserHandle mUserHandle;
    140         private String mSettingsActivity;
    141         private String mUserRestriction;
    142 
    143         public Builder setPackageName(String packageName) {
    144             mPackageName = packageName;
    145             return this;
    146         }
    147 
    148         public Builder setClassName(String className) {
    149             mClassName = className;
    150             return this;
    151         }
    152 
    153         public Builder setTitle(String title) {
    154             mTitle = title;
    155             return this;
    156         }
    157 
    158         public Builder setIconId(int iconId) {
    159             mIconId = iconId;
    160             return this;
    161         }
    162 
    163         public Builder setUserHandle(UserHandle userHandle) {
    164             mUserHandle = userHandle;
    165             return this;
    166         }
    167 
    168         public Builder setSettingsActivity(String settingsActivity) {
    169             mSettingsActivity = settingsActivity;
    170             return this;
    171         }
    172 
    173         public Builder setUserRestriction(String userRestriction) {
    174             mUserRestriction = userRestriction;
    175             return this;
    176         }
    177 
    178         public InjectedSetting build() {
    179             if (mPackageName == null || mClassName == null || TextUtils.isEmpty(mTitle)
    180                     || TextUtils.isEmpty(mSettingsActivity)) {
    181                 if (Log.isLoggable(SettingsInjector.TAG, Log.WARN)) {
    182                     Log.w(SettingsInjector.TAG, "Illegal setting specification: package="
    183                             + mPackageName + ", class=" + mClassName
    184                             + ", title=" + mTitle + ", settingsActivity=" + mSettingsActivity);
    185                 }
    186                 return null;
    187             }
    188             return new InjectedSetting(this);
    189         }
    190     }
    191 }
    192