Home | History | Annotate | Download | only in search2
      1 /*
      2  * Copyright (C) 2016 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.search2;
     19 
     20 import android.content.Context;
     21 import android.util.Pair;
     22 import android.view.View;
     23 import android.widget.Switch;
     24 
     25 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     26 import com.android.settings.R;
     27 
     28 /**
     29  * ViewHolder for Settings represented as SwitchPreferences.
     30  */
     31 public class InlineSwitchViewHolder extends SearchViewHolder {
     32 
     33     public final Switch switchView;
     34 
     35     private final Context mContext;
     36 
     37     public InlineSwitchViewHolder(View view, Context context) {
     38         super(view);
     39         mContext = context;
     40         switchView = view.findViewById(R.id.switchView);
     41     }
     42 
     43     @Override
     44     public void onBind(SearchFragment fragment, SearchResult result) {
     45         super.onBind(fragment, result);
     46         if (mContext == null) {
     47             return;
     48         }
     49         final InlineSwitchPayload payload = (InlineSwitchPayload) result.payload;
     50         switchView.setChecked(payload.getSwitchValue(mContext));
     51         switchView.setOnCheckedChangeListener((buttonView, isChecked) -> {
     52             final Pair<Integer, Object> name = Pair.create(
     53                     MetricsEvent.FIELD_SETTINGS_SEARCH_INLINE_RESULT_NAME, payload.settingsUri);
     54             final Pair<Integer, Object> value = Pair.create(
     55                     MetricsEvent.FIELD_SETTINGS_SEARCH_INLINE_RESULT_VALUE, isChecked
     56                             ? "checked"
     57                             : "not-checked");
     58             final Pair<Integer, Object> rank = Pair.create(
     59                     MetricsEvent.FIELD_SETTINGS_SERACH_RESULT_RANK, getAdapterPosition());
     60             mMetricsFeatureProvider.action(mContext,
     61                     MetricsEvent.ACTION_CLICK_SETTINGS_SEARCH_INLINE_RESULT,
     62                     name, value, rank);
     63 
     64             fragment.onSearchResultClicked();
     65             payload.setSwitchValue(mContext, isChecked);
     66         });
     67     }
     68 }
     69