Home | History | Annotate | Download | only in globalactions
      1 /*
      2  * Copyright (C) 2019 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 package com.android.systemui.globalactions;
     18 
     19 import android.content.Context;
     20 import android.text.TextUtils;
     21 import android.util.AttributeSet;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 
     25 import com.android.internal.annotations.VisibleForTesting;
     26 import com.android.systemui.HardwareBgDrawable;
     27 import com.android.systemui.MultiListLayout;
     28 import com.android.systemui.R;
     29 import com.android.systemui.util.leak.RotationUtils;
     30 
     31 import java.util.Locale;
     32 
     33 /**
     34  * Grid-based implementation of the button layout created by the global actions dialog.
     35  */
     36 public abstract class GlobalActionsLayout extends MultiListLayout {
     37 
     38     boolean mBackgroundsSet;
     39 
     40     public GlobalActionsLayout(Context context, AttributeSet attrs) {
     41         super(context, attrs);
     42     }
     43 
     44     private void setBackgrounds() {
     45         int gridBackgroundColor = getResources().getColor(
     46                 R.color.global_actions_grid_background, null);
     47         int separatedBackgroundColor = getResources().getColor(
     48                 R.color.global_actions_separated_background, null);
     49         HardwareBgDrawable listBackground  = new HardwareBgDrawable(true, true, getContext());
     50         HardwareBgDrawable separatedBackground = new HardwareBgDrawable(true, true, getContext());
     51         listBackground.setTint(gridBackgroundColor);
     52         separatedBackground.setTint(separatedBackgroundColor);
     53         getListView().setBackground(listBackground);
     54         getSeparatedView().setBackground(separatedBackground);
     55     }
     56 
     57     @Override
     58     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     59         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     60 
     61         // backgrounds set only once, the first time onMeasure is called after inflation
     62         if (getListView() != null && !mBackgroundsSet) {
     63             setBackgrounds();
     64             mBackgroundsSet = true;
     65         }
     66     }
     67 
     68     protected void addToListView(View v, boolean reverse) {
     69         if (reverse) {
     70             getListView().addView(v, 0);
     71         } else {
     72             getListView().addView(v);
     73         }
     74     }
     75 
     76     protected void addToSeparatedView(View v, boolean reverse) {
     77         if (reverse) {
     78             getSeparatedView().addView(v, 0);
     79         } else {
     80             getSeparatedView().addView(v);
     81         }
     82     }
     83 
     84     @VisibleForTesting
     85     protected int getCurrentLayoutDirection() {
     86         return TextUtils.getLayoutDirectionFromLocale(Locale.getDefault());
     87     }
     88 
     89     @VisibleForTesting
     90     protected int getCurrentRotation() {
     91         return RotationUtils.getRotation(mContext);
     92     }
     93 
     94     /**
     95      * Determines whether the ListGridLayout should reverse the ordering of items within sublists.
     96      * Used for RTL languages to ensure that items appear in the same positions, without having to
     97      * override layoutDirection, which breaks Talkback ordering.
     98      */
     99     protected abstract boolean shouldReverseListItems();
    100 
    101     @Override
    102     public void onUpdateList() {
    103         super.onUpdateList();
    104 
    105         ViewGroup separatedView = getSeparatedView();
    106         ViewGroup listView = getListView();
    107 
    108         for (int i = 0; i < mAdapter.getCount(); i++) {
    109             // generate the view item
    110             View v;
    111             boolean separated = mAdapter.shouldBeSeparated(i);
    112             if (separated) {
    113                 v = mAdapter.getView(i, null, separatedView);
    114             } else {
    115                 v = mAdapter.getView(i, null, listView);
    116             }
    117             if (separated) {
    118                 addToSeparatedView(v, false);
    119             } else {
    120                 addToListView(v, shouldReverseListItems());
    121             }
    122         }
    123     }
    124 
    125     @Override
    126     protected ViewGroup getSeparatedView() {
    127         return findViewById(R.id.separated_button);
    128     }
    129 
    130     @Override
    131     protected ViewGroup getListView() {
    132         return findViewById(android.R.id.list);
    133     }
    134 
    135     protected View getWrapper() {
    136         return getChildAt(0);
    137     }
    138 
    139     /**
    140      * Not used in this implementation of the Global Actions Menu, but necessary for some others.
    141      */
    142     @Override
    143     public void setDivisionView(View v) {
    144         // do nothing
    145     }
    146 }
    147