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.sidebar; 18 19 import android.app.Activity; 20 import android.os.Looper; 21 import android.view.View; 22 import android.view.View.OnDragListener; 23 import android.view.ViewGroup; 24 import android.widget.ArrayAdapter; 25 26 import com.android.documentsui.R; 27 28 import java.util.HashMap; 29 import java.util.List; 30 import java.util.Map; 31 32 /** 33 * The adapter for the {@link android.widget.ListView} in sidebar. It contains the list of 34 * {@link Item}s and provides sub-views to {@link android.widget.ListView}. 35 */ 36 class RootsAdapter extends ArrayAdapter<Item> { 37 private static final Map<String, Long> sIdMap = new HashMap<>(); 38 // the next available id to associate with a new string id 39 private static long sNextAvailableId; 40 41 private final OnDragListener mDragListener; 42 43 public RootsAdapter( 44 Activity activity, 45 List<Item> items, 46 OnDragListener dragListener) { 47 super(activity, 0, items); 48 49 mDragListener = dragListener; 50 } 51 52 @Override 53 public boolean hasStableIds() { 54 return true; 55 } 56 57 @Override 58 public long getItemId(int position) { 59 // Ensure this method is only called in main thread because we don't have any 60 // concurrency protection. 61 assert(Looper.myLooper() == Looper.getMainLooper()); 62 63 String stringId = getItem(position).stringId; 64 65 long id; 66 if (sIdMap.containsKey(stringId)) { 67 id = sIdMap.get(stringId); 68 } else { 69 id = sNextAvailableId++; 70 sIdMap.put(stringId, id); 71 } 72 73 return id; 74 } 75 76 @Override 77 public View getView(int position, View convertView, ViewGroup parent) { 78 final Item item = getItem(position); 79 final View view = item.getView(convertView, parent); 80 81 if (item.isRoot()) { 82 view.setTag(R.id.item_position_tag, position); 83 view.setOnDragListener(mDragListener); 84 } else { 85 view.setTag(R.id.item_position_tag, null); 86 view.setOnDragListener(null); 87 } 88 return view; 89 } 90 91 @Override 92 public boolean areAllItemsEnabled() { 93 return false; 94 } 95 96 @Override 97 public boolean isEnabled(int position) { 98 return getItemViewType(position) != 1; 99 } 100 101 @Override 102 public int getItemViewType(int position) { 103 final Item item = getItem(position); 104 if (item instanceof RootItem || item instanceof AppItem) { 105 return 0; 106 } else { 107 return 1; 108 } 109 } 110 111 @Override 112 public int getViewTypeCount() { 113 return 2; 114 } 115 } 116