1 /* 2 * Copyright (C) 2018 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.internal.globalactions; 17 18 import android.content.Context; 19 import android.view.LayoutInflater; 20 import android.view.View; 21 import android.view.ViewGroup; 22 import android.widget.BaseAdapter; 23 import java.util.function.BooleanSupplier; 24 import java.util.List; 25 26 /** 27 * The adapter used for the list within the global actions dialog, taking into account whether the 28 * keyguard is showing via {@link LegacyGlobalActions#mKeyguardShowing} and whether the device is 29 * provisioned via {@link LegacyGlobalActions#mDeviceProvisioned}. 30 */ 31 public class ActionsAdapter extends BaseAdapter { 32 private final Context mContext; 33 private final List<Action> mItems; 34 private final BooleanSupplier mDeviceProvisioned; 35 private final BooleanSupplier mKeyguardShowing; 36 ActionsAdapter(Context context, List<Action> items, BooleanSupplier deviceProvisioned, BooleanSupplier keyguardShowing)37 public ActionsAdapter(Context context, List<Action> items, 38 BooleanSupplier deviceProvisioned, BooleanSupplier keyguardShowing) { 39 mContext = context; 40 mItems = items; 41 mDeviceProvisioned = deviceProvisioned; 42 mKeyguardShowing = keyguardShowing; 43 } 44 45 @Override getCount()46 public int getCount() { 47 final boolean keyguardShowing = mKeyguardShowing.getAsBoolean(); 48 final boolean deviceProvisioned = mDeviceProvisioned.getAsBoolean(); 49 int count = 0; 50 51 for (int i = 0; i < mItems.size(); i++) { 52 final Action action = mItems.get(i); 53 54 if (keyguardShowing && !action.showDuringKeyguard()) { 55 continue; 56 } 57 if (!deviceProvisioned && !action.showBeforeProvisioning()) { 58 continue; 59 } 60 count++; 61 } 62 return count; 63 } 64 65 @Override isEnabled(int position)66 public boolean isEnabled(int position) { 67 return getItem(position).isEnabled(); 68 } 69 70 @Override areAllItemsEnabled()71 public boolean areAllItemsEnabled() { 72 return false; 73 } 74 75 @Override getItem(int position)76 public Action getItem(int position) { 77 final boolean keyguardShowing = mKeyguardShowing.getAsBoolean(); 78 final boolean deviceProvisioned = mDeviceProvisioned.getAsBoolean(); 79 80 int filteredPos = 0; 81 for (int i = 0; i < mItems.size(); i++) { 82 final Action action = mItems.get(i); 83 if (keyguardShowing && !action.showDuringKeyguard()) { 84 continue; 85 } 86 if (!deviceProvisioned && !action.showBeforeProvisioning()) { 87 continue; 88 } 89 if (filteredPos == position) { 90 return action; 91 } 92 filteredPos++; 93 } 94 95 throw new IllegalArgumentException("position " + position 96 + " out of range of showable actions" 97 + ", filtered count=" + getCount() 98 + ", keyguardshowing=" + keyguardShowing 99 + ", provisioned=" + deviceProvisioned); 100 } 101 102 @Override getItemId(int position)103 public long getItemId(int position) { 104 return position; 105 } 106 107 @Override getView(int position, View convertView, ViewGroup parent)108 public View getView(int position, View convertView, ViewGroup parent) { 109 Action action = getItem(position); 110 return action.create(mContext, convertView, parent, LayoutInflater.from(mContext)); 111 } 112 } 113