Home | History | Annotate | Download | only in slices
      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  */
     16 
     17 package com.android.settings.slices;
     18 
     19 import android.annotation.IntDef;
     20 import android.net.Uri;
     21 import android.text.TextUtils;
     22 
     23 import java.lang.annotation.Retention;
     24 import java.lang.annotation.RetentionPolicy;
     25 import java.util.List;
     26 
     27 /**
     28  * Data class representing a slice stored by {@link SlicesIndexer}.
     29  * Note that {@link #mKey} is treated as a primary key for this class and determines equality.
     30  */
     31 public class SliceData {
     32 
     33     /**
     34      * Flags indicating the UI type of the Slice.
     35      */
     36     @IntDef({SliceType.INTENT, SliceType.SWITCH, SliceType.SLIDER})
     37     @Retention(RetentionPolicy.SOURCE)
     38     public @interface SliceType {
     39         /**
     40          * Only supports content intent.
     41          */
     42         int INTENT = 0;
     43 
     44         /**
     45          * Supports toggle action.
     46          */
     47         int SWITCH = 1;
     48 
     49         /**
     50          * Supports progress bar.
     51          */
     52         int SLIDER = 2;
     53     }
     54 
     55     private final String mKey;
     56 
     57     private final String mTitle;
     58 
     59     private final String mSummary;
     60 
     61     private final CharSequence mScreenTitle;
     62 
     63     private final String mKeywords;
     64 
     65     private final int mIconResource;
     66 
     67     private final String mFragmentClassName;
     68 
     69     private final Uri mUri;
     70 
     71     private final String mPreferenceController;
     72 
     73     @SliceType
     74     private final int mSliceType;
     75 
     76     private final boolean mIsPlatformDefined;
     77 
     78     public String getKey() {
     79         return mKey;
     80     }
     81 
     82     public String getTitle() {
     83         return mTitle;
     84     }
     85 
     86     public String getSummary() {
     87         return mSummary;
     88     }
     89 
     90     public CharSequence getScreenTitle() {
     91         return mScreenTitle;
     92     }
     93 
     94     public String getKeywords() {
     95         return mKeywords;
     96     }
     97 
     98     public int getIconResource() {
     99         return mIconResource;
    100     }
    101 
    102     public String getFragmentClassName() {
    103         return mFragmentClassName;
    104     }
    105 
    106     public Uri getUri() {
    107         return mUri;
    108     }
    109 
    110     public String getPreferenceController() {
    111         return mPreferenceController;
    112     }
    113 
    114     public int getSliceType() {
    115         return mSliceType;
    116     }
    117 
    118     public boolean isPlatformDefined() {
    119         return mIsPlatformDefined;
    120     }
    121 
    122     private SliceData(Builder builder) {
    123         mKey = builder.mKey;
    124         mTitle = builder.mTitle;
    125         mSummary = builder.mSummary;
    126         mScreenTitle = builder.mScreenTitle;
    127         mKeywords = builder.mKeywords;
    128         mIconResource = builder.mIconResource;
    129         mFragmentClassName = builder.mFragmentClassName;
    130         mUri = builder.mUri;
    131         mPreferenceController = builder.mPrefControllerClassName;
    132         mSliceType = builder.mSliceType;
    133         mIsPlatformDefined = builder.mIsPlatformDefined;
    134     }
    135 
    136     @Override
    137     public int hashCode() {
    138         return mKey.hashCode();
    139     }
    140 
    141     @Override
    142     public boolean equals(Object obj) {
    143         if (!(obj instanceof SliceData)) {
    144             return false;
    145         }
    146         SliceData newObject = (SliceData) obj;
    147         return TextUtils.equals(mKey, newObject.mKey);
    148     }
    149 
    150     static class Builder {
    151         private String mKey;
    152 
    153         private String mTitle;
    154 
    155         private String mSummary;
    156 
    157         private CharSequence mScreenTitle;
    158 
    159         private String mKeywords;
    160 
    161         private int mIconResource;
    162 
    163         private String mFragmentClassName;
    164 
    165         private Uri mUri;
    166 
    167         private String mPrefControllerClassName;
    168 
    169         private int mSliceType;
    170 
    171         private boolean mIsPlatformDefined;
    172 
    173         public Builder setKey(String key) {
    174             mKey = key;
    175             return this;
    176         }
    177 
    178         public Builder setTitle(String title) {
    179             mTitle = title;
    180             return this;
    181         }
    182 
    183         public Builder setSummary(String summary) {
    184             mSummary = summary;
    185             return this;
    186         }
    187 
    188         public Builder setScreenTitle(CharSequence screenTitle) {
    189             mScreenTitle = screenTitle;
    190             return this;
    191         }
    192 
    193         public Builder setKeywords(String keywords) {
    194             mKeywords = keywords;
    195             return this;
    196         }
    197 
    198         public Builder setIcon(int iconResource) {
    199             mIconResource = iconResource;
    200             return this;
    201         }
    202 
    203         public Builder setPreferenceControllerClassName(String controllerClassName) {
    204             mPrefControllerClassName = controllerClassName;
    205             return this;
    206         }
    207 
    208         public Builder setFragmentName(String fragmentClassName) {
    209             mFragmentClassName = fragmentClassName;
    210             return this;
    211         }
    212 
    213         public Builder setUri(Uri uri) {
    214             mUri = uri;
    215             return this;
    216         }
    217 
    218         public Builder setSliceType(@SliceType int sliceType) {
    219             mSliceType = sliceType;
    220             return this;
    221         }
    222 
    223         public Builder setPlatformDefined(boolean isPlatformDefined) {
    224             mIsPlatformDefined = isPlatformDefined;
    225             return this;
    226         }
    227 
    228         public SliceData build() {
    229             if (TextUtils.isEmpty(mKey)) {
    230                 throw new InvalidSliceDataException("Key cannot be empty");
    231             }
    232 
    233             if (TextUtils.isEmpty(mTitle)) {
    234                 throw new InvalidSliceDataException("Title cannot be empty");
    235             }
    236 
    237             if (TextUtils.isEmpty(mFragmentClassName)) {
    238                 throw new InvalidSliceDataException("Fragment Name cannot be empty");
    239             }
    240 
    241             if (TextUtils.isEmpty(mPrefControllerClassName)) {
    242                 throw new InvalidSliceDataException("Preference Controller cannot be empty");
    243             }
    244 
    245             return new SliceData(this);
    246         }
    247 
    248         public String getKey() {
    249             return mKey;
    250         }
    251     }
    252 
    253     public static class InvalidSliceDataException extends RuntimeException {
    254 
    255         public InvalidSliceDataException(String message) {
    256             super(message);
    257         }
    258     }
    259 }