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