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 package com.android.documentsui; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.view.accessibility.AccessibilityEvent; 25 import android.widget.AdapterView; 26 import android.widget.BaseAdapter; 27 import android.widget.Spinner; 28 import android.widget.TextView; 29 30 import com.android.documentsui.NavigationViewManager.Breadcrumb; 31 import com.android.documentsui.NavigationViewManager.Environment; 32 import com.android.documentsui.base.DocumentInfo; 33 import com.android.documentsui.base.RootInfo; 34 import com.android.documentsui.base.State; 35 36 import java.util.function.IntConsumer; 37 38 /** 39 * Dropdown implementation of breadcrumb used for phone device layouts 40 */ 41 42 public final class DropdownBreadcrumb extends Spinner implements Breadcrumb { 43 44 private DropdownAdapter mAdapter; 45 DropdownBreadcrumb( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)46 public DropdownBreadcrumb( 47 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 48 super(context, attrs, defStyleAttr, defStyleRes); 49 } 50 DropdownBreadcrumb(Context context, AttributeSet attrs, int defStyleAttr)51 public DropdownBreadcrumb(Context context, AttributeSet attrs, int defStyleAttr) { 52 super(context, attrs, defStyleAttr); 53 } 54 DropdownBreadcrumb(Context context, AttributeSet attrs)55 public DropdownBreadcrumb(Context context, AttributeSet attrs) { 56 super(context, attrs); 57 } 58 DropdownBreadcrumb(Context context)59 public DropdownBreadcrumb(Context context) { 60 super(context); 61 } 62 63 @Override setup(Environment env, State state, IntConsumer listener)64 public void setup(Environment env, State state, IntConsumer listener) { 65 mAdapter = new DropdownAdapter(state, env); 66 setOnItemSelectedListener( 67 new OnItemSelectedListener() { 68 @Override 69 public void onItemSelected( 70 AdapterView<?> parent, View view, int position, long id) { 71 listener.accept(position); 72 } 73 74 @Override 75 public void onNothingSelected(AdapterView<?> parent) {} 76 }); 77 setAccessibilityDelegate(new AccessibilityDelegate() { 78 @Override 79 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) { 80 if (event.getEventType() != AccessibilityEvent.TYPE_VIEW_SELECTED) { 81 super.onInitializeAccessibilityEvent(host, event); 82 } 83 } 84 }); 85 } 86 87 @Override show(boolean visibility)88 public void show(boolean visibility) { 89 if (visibility) { 90 setVisibility(VISIBLE); 91 setAdapter(mAdapter); 92 } else { 93 setVisibility(GONE); 94 setAdapter(null); 95 } 96 } 97 98 @Override postUpdate()99 public void postUpdate() { 100 setSelection(mAdapter.getCount() - 1, false); 101 } 102 103 private static final class DropdownAdapter extends BaseAdapter { 104 private Environment mEnv; 105 private State mState; 106 DropdownAdapter(State state, Environment env)107 public DropdownAdapter(State state, Environment env) { 108 mState = state; 109 mEnv = env; 110 } 111 112 @Override getCount()113 public int getCount() { 114 return mState.stack.size(); 115 } 116 117 @Override getItem(int position)118 public DocumentInfo getItem(int position) { 119 return mState.stack.get(position); 120 } 121 122 @Override getItemId(int position)123 public long getItemId(int position) { 124 return position; 125 } 126 127 @Override getView(int position, View convertView, ViewGroup parent)128 public View getView(int position, View convertView, ViewGroup parent) { 129 if (convertView == null) { 130 convertView = LayoutInflater.from(parent.getContext()) 131 .inflate(R.layout.item_subdir_title, parent, false); 132 } 133 134 final TextView title = (TextView) convertView.findViewById(android.R.id.title); 135 final DocumentInfo doc = getItem(position); 136 137 if (position == 0) { 138 final RootInfo root = mEnv.getCurrentRoot(); 139 title.setText(root.title); 140 } else { 141 title.setText(doc.displayName); 142 } 143 144 return convertView; 145 } 146 147 @Override getDropDownView(int position, View convertView, ViewGroup parent)148 public View getDropDownView(int position, View convertView, ViewGroup parent) { 149 if (convertView == null) { 150 convertView = LayoutInflater.from(parent.getContext()) 151 .inflate(R.layout.item_subdir, parent, false); 152 } 153 154 final TextView title = (TextView) convertView.findViewById(android.R.id.title); 155 final DocumentInfo doc = getItem(position); 156 157 if (position == 0) { 158 final RootInfo root = mEnv.getCurrentRoot(); 159 title.setText(root.title); 160 } else { 161 title.setText(doc.displayName); 162 } 163 164 return convertView; 165 } 166 } 167 168 } 169