1 /* 2 * Copyright (C) 2015 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 package com.android.settings.dashboard.conditional; 17 18 import android.content.Context; 19 import android.support.v7.widget.RecyclerView; 20 import android.support.v7.widget.helper.ItemTouchHelper; 21 import android.util.Log; 22 import android.view.View; 23 import android.widget.Button; 24 import android.widget.ImageView; 25 26 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 27 import com.android.settings.R; 28 import com.android.settings.dashboard.DashboardAdapter; 29 import com.android.settings.overlay.FeatureFactory; 30 import com.android.settingslib.WirelessUtils; 31 32 public class ConditionAdapterUtils { 33 private static final String TAG = "ConditionAdapterUtils"; 34 addDismiss(final RecyclerView recyclerView)35 public static void addDismiss(final RecyclerView recyclerView) { 36 ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(0, 37 ItemTouchHelper.START | ItemTouchHelper.END) { 38 @Override 39 public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, 40 RecyclerView.ViewHolder target) { 41 return true; 42 } 43 44 @Override 45 public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { 46 return viewHolder.getItemViewType() == R.layout.condition_card 47 ? super.getSwipeDirs(recyclerView, viewHolder) : 0; 48 } 49 50 @Override 51 public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { 52 DashboardAdapter adapter = (DashboardAdapter) recyclerView.getAdapter(); 53 Object item = adapter.getItem(viewHolder.getItemId()); 54 if (item instanceof Condition) { 55 ((Condition) item).silence(); 56 } 57 } 58 }; 59 ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback); 60 itemTouchHelper.attachToRecyclerView(recyclerView); 61 } 62 bindViews(final Condition condition, DashboardAdapter.DashboardItemHolder view, boolean isExpanded, View.OnClickListener onClickListener, View.OnClickListener onExpandListener)63 public static void bindViews(final Condition condition, 64 DashboardAdapter.DashboardItemHolder view, boolean isExpanded, 65 View.OnClickListener onClickListener, View.OnClickListener onExpandListener) { 66 if (condition instanceof AirplaneModeCondition) { 67 Log.d(TAG, "Airplane mode condition has been bound with " 68 + "isActive=" + condition.isActive() + ". Airplane mode is currently " + 69 WirelessUtils.isAirplaneModeOn(condition.mManager.getContext())); 70 } 71 View card = view.itemView.findViewById(R.id.content); 72 card.setTag(condition); 73 card.setOnClickListener(onClickListener); 74 view.icon.setImageIcon(condition.getIcon()); 75 view.title.setText(condition.getTitle()); 76 final View collapsedGroup = view.itemView.findViewById(R.id.collapsed_group); 77 collapsedGroup.setTag(condition); 78 final ImageView expand = (ImageView) view.itemView.findViewById(R.id.expand_indicator); 79 expand.setImageResource(isExpanded ? R.drawable.ic_expand_less : R.drawable.ic_expand_more); 80 expand.setContentDescription(expand.getContext().getString(isExpanded 81 ? R.string.condition_expand_hide : R.string.condition_expand_show)); 82 collapsedGroup.setOnClickListener(onExpandListener); 83 84 View detailGroup = view.itemView.findViewById(R.id.detail_group); 85 CharSequence[] actions = condition.getActions(); 86 if (isExpanded != (detailGroup.getVisibility() == View.VISIBLE)) { 87 if (isExpanded) { 88 final boolean hasButtons = actions.length > 0; 89 setViewVisibility(detailGroup, R.id.divider, hasButtons); 90 setViewVisibility(detailGroup, R.id.buttonBar, hasButtons); 91 92 detailGroup.setVisibility(View.VISIBLE); 93 } else { 94 detailGroup.setVisibility(View.GONE); 95 } 96 } 97 98 if (isExpanded) { 99 view.summary.setText(condition.getSummary()); 100 for (int i = 0; i < 2; i++) { 101 Button button = (Button) detailGroup.findViewById(i == 0 102 ? R.id.first_action : R.id.second_action); 103 if (actions.length > i) { 104 button.setVisibility(View.VISIBLE); 105 button.setText(actions[i]); 106 final int index = i; 107 button.setOnClickListener(new View.OnClickListener() { 108 @Override 109 public void onClick(View v) { 110 Context context = v.getContext(); 111 FeatureFactory.getFactory(context).getMetricsFeatureProvider() 112 .action(context, MetricsEvent.ACTION_SETTINGS_CONDITION_BUTTON, 113 condition.getMetricsConstant()); 114 condition.onActionClick(index); 115 } 116 }); 117 } else { 118 button.setVisibility(View.GONE); 119 } 120 } 121 } 122 } 123 setViewVisibility(View containerView, int viewId, boolean visible)124 private static void setViewVisibility(View containerView, int viewId, boolean visible) { 125 View view = containerView.findViewById(viewId); 126 if (view != null) { 127 view.setVisibility(visible ? View.VISIBLE : View.GONE); 128 } 129 } 130 } 131