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.animation.Animator; 19 import android.animation.AnimatorListenerAdapter; 20 import android.animation.ObjectAnimator; 21 import android.support.v7.widget.RecyclerView; 22 import android.support.v7.widget.helper.ItemTouchHelper; 23 import android.view.View; 24 import android.view.View.OnLayoutChangeListener; 25 import android.view.ViewGroup.LayoutParams; 26 import android.widget.Button; 27 import android.widget.ImageView; 28 import com.android.internal.logging.MetricsLogger; 29 import com.android.internal.logging.MetricsProto.MetricsEvent; 30 import com.android.settings.R; 31 import com.android.settings.dashboard.DashboardAdapter; 32 33 public class ConditionAdapterUtils { 34 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 63 public static void bindViews(final Condition condition, 64 DashboardAdapter.DashboardItemHolder view, boolean isExpanded, 65 View.OnClickListener onClickListener, View.OnClickListener onExpandListener) { 66 View card = view.itemView.findViewById(R.id.content); 67 card.setTag(condition); 68 card.setOnClickListener(onClickListener); 69 view.icon.setImageIcon(condition.getIcon()); 70 view.title.setText(condition.getTitle()); 71 ImageView expand = (ImageView) view.itemView.findViewById(R.id.expand_indicator); 72 expand.setTag(condition); 73 expand.setImageResource(isExpanded ? R.drawable.ic_expand_less : R.drawable.ic_expand_more); 74 expand.setContentDescription(expand.getContext().getString(isExpanded 75 ? R.string.condition_expand_hide : R.string.condition_expand_show)); 76 expand.setOnClickListener(onExpandListener); 77 78 View detailGroup = view.itemView.findViewById(R.id.detail_group); 79 CharSequence[] actions = condition.getActions(); 80 if (isExpanded != (detailGroup.getVisibility() == View.VISIBLE)) { 81 animateChange(view.itemView, view.itemView.findViewById(R.id.content), 82 detailGroup, isExpanded, actions.length > 0); 83 } 84 if (isExpanded) { 85 view.summary.setText(condition.getSummary()); 86 for (int i = 0; i < 2; i++) { 87 Button button = (Button) detailGroup.findViewById(i == 0 88 ? R.id.first_action : R.id.second_action); 89 if (actions.length > i) { 90 button.setVisibility(View.VISIBLE); 91 button.setText(actions[i]); 92 final int index = i; 93 button.setOnClickListener(new View.OnClickListener() { 94 @Override 95 public void onClick(View v) { 96 MetricsLogger.action(v.getContext(), 97 MetricsEvent.ACTION_SETTINGS_CONDITION_BUTTON, 98 condition.getMetricsConstant()); 99 condition.onActionClick(index); 100 } 101 }); 102 } else { 103 button.setVisibility(View.GONE); 104 } 105 } 106 } 107 } 108 109 private static void animateChange(final View view, final View content, 110 final View detailGroup, final boolean visible, final boolean hasButtons) { 111 setViewVisibility(detailGroup, R.id.divider, hasButtons); 112 setViewVisibility(detailGroup, R.id.buttonBar, hasButtons); 113 final int beforeBottom = content.getBottom(); 114 setHeight(detailGroup, visible ? LayoutParams.WRAP_CONTENT : 0); 115 detailGroup.setVisibility(View.VISIBLE); 116 view.addOnLayoutChangeListener(new OnLayoutChangeListener() { 117 public static final long DURATION = 250; 118 119 @Override 120 public void onLayoutChange(View v, int left, int top, int right, int bottom, 121 int oldLeft, int oldTop, int oldRight, int oldBottom) { 122 final int afterBottom = content.getBottom(); 123 v.removeOnLayoutChangeListener(this); 124 final ObjectAnimator animator = ObjectAnimator.ofInt(content, "bottom", 125 beforeBottom, afterBottom); 126 animator.setDuration(DURATION); 127 animator.addListener(new AnimatorListenerAdapter() { 128 @Override 129 public void onAnimationEnd(Animator animation) { 130 if (!visible) { 131 detailGroup.setVisibility(View.GONE); 132 } 133 } 134 }); 135 animator.start(); 136 } 137 }); 138 } 139 140 private static void setHeight(View detailGroup, int height) { 141 final LayoutParams params = detailGroup.getLayoutParams(); 142 params.height = height; 143 detailGroup.setLayoutParams(params); 144 } 145 146 private static void setViewVisibility(View containerView, int viewId, boolean visible) { 147 View view = containerView.findViewById(viewId); 148 if (view != null) { 149 view.setVisibility(visible ? View.VISIBLE : View.GONE); 150 } 151 } 152 } 153