Home | History | Annotate | Download | only in search
      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 
     18 package com.android.settings.search;
     19 
     20 import android.content.Intent;
     21 import android.os.Parcel;
     22 import android.os.Parcelable;
     23 
     24 /**
     25  * Payload for inline Switch results. Mappings from integer to boolean.
     26  */
     27 public class InlineSwitchPayload extends InlinePayload {
     28 
     29     private static final int ON = 1;
     30     private static final int OFF = 0;
     31 
     32     /**
     33      * Provides a mapping for how switches are stored.
     34      * If mIsStandard is true, then (0 == false) and (1 == true)
     35      * If mIsStandard is false, then (1 == false) and (0 == true)
     36      */
     37     private boolean mIsStandard;
     38 
     39     /**
     40      *
     41      * @param key uniquely identifies the stored setting.
     42      * @param source of the setting. Used to determine where to get and set the setting.
     43      * @param onValue is the value stored as on for the switch. Should be 0 or 1.
     44      * @param intent to the setting page.
     45      * @param isDeviceSupported is true when the setting is valid for the given device.
     46      */
     47     public InlineSwitchPayload(String key, @SettingsSource int source,
     48             int onValue, Intent intent, boolean isDeviceSupported, int defaultValue) {
     49         super(key, source, intent, isDeviceSupported, defaultValue);
     50         // If on is stored as TRUE then the switch is standard.
     51         mIsStandard = onValue == TRUE;
     52     }
     53 
     54     private InlineSwitchPayload(Parcel in) {
     55         super(in);
     56         mIsStandard = in.readInt() == TRUE;
     57     }
     58 
     59     @Override
     60     @PayloadType public int getType() {
     61         return PayloadType.INLINE_SWITCH;
     62     }
     63 
     64     @Override
     65     protected int standardizeInput(int value) {
     66         if (value != OFF && value != ON) {
     67             throw new IllegalArgumentException("Invalid input for InlineSwitch. Expected: "
     68                     + ON + " or " + OFF
     69                     + " but found: " + value);
     70         }
     71         return mIsStandard
     72                 ? value
     73                 : 1 - value;
     74     }
     75 
     76     @Override
     77     public void writeToParcel(Parcel dest, int flags) {
     78         super.writeToParcel(dest, flags);
     79         dest.writeInt(mIsStandard ? TRUE : FALSE);
     80     }
     81 
     82     public static final Parcelable.Creator<InlineSwitchPayload> CREATOR =
     83             new Parcelable.Creator<InlineSwitchPayload>() {
     84         @Override
     85         public InlineSwitchPayload createFromParcel(Parcel in) {
     86             return new InlineSwitchPayload(in);
     87         }
     88 
     89         @Override
     90         public InlineSwitchPayload[] newArray(int size) {
     91             return new InlineSwitchPayload[size];
     92         }
     93     };
     94 
     95     public boolean isStandard() {
     96         return mIsStandard;
     97     }
     98 }
     99