Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 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; |
Tony Huang | cd45661 | 2019-04-15 16:50:10 +0800 | [diff] [blame^] | 24 | import android.view.accessibility.AccessibilityEvent; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 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; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 32 | import com.android.documentsui.base.DocumentInfo; |
| 33 | import com.android.documentsui.base.RootInfo; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 34 | import com.android.documentsui.base.State; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 35 | |
Ben Lin | 6244245 | 2017-01-19 15:10:49 -0800 | [diff] [blame] | 36 | import java.util.function.IntConsumer; |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 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 | |
| 46 | public DropdownBreadcrumb( |
| 47 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 48 | super(context, attrs, defStyleAttr, defStyleRes); |
| 49 | } |
| 50 | |
| 51 | public DropdownBreadcrumb(Context context, AttributeSet attrs, int defStyleAttr) { |
| 52 | super(context, attrs, defStyleAttr); |
| 53 | } |
| 54 | |
| 55 | public DropdownBreadcrumb(Context context, AttributeSet attrs) { |
| 56 | super(context, attrs); |
| 57 | } |
| 58 | |
| 59 | public DropdownBreadcrumb(Context context) { |
| 60 | super(context); |
| 61 | } |
| 62 | |
| 63 | @Override |
Ben Lin | 6244245 | 2017-01-19 15:10:49 -0800 | [diff] [blame] | 64 | public void setup(Environment env, State state, IntConsumer listener) { |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 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 | }); |
Tony Huang | cd45661 | 2019-04-15 16:50:10 +0800 | [diff] [blame^] | 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 | }); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 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 |
| 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 | |
| 107 | public DropdownAdapter(State state, Environment env) { |
| 108 | mState = state; |
| 109 | mEnv = env; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public int getCount() { |
| 114 | return mState.stack.size(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public DocumentInfo getItem(int position) { |
Garfield Tan | 2a83742 | 2016-10-19 11:50:45 -0700 | [diff] [blame] | 119 | return mState.stack.get(position); |
Ben Lin | b8c54e7 | 2016-06-10 12:13:27 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public long getItemId(int position) { |
| 124 | return position; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 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 |
| 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 | } |