Home | History | Annotate | Download | only in drawer
      1 /*
      2  * Copyright (C) 2017 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 package com.android.car.media.drawer;
     17 
     18 import com.android.car.app.CarDrawerActivity;
     19 import com.android.car.app.CarDrawerAdapter;
     20 import com.android.car.app.DrawerItemViewHolder;
     21 
     22 /**
     23  * Subclass of CarDrawerAdapter used by the Media app.
     24  * <p>
     25  * This adapter delegates actual fetching of items (and other operations) to a
     26  * {@link MediaItemsFetcher}. The current fetcher being used can be updated at runtime.
     27  */
     28 class MediaDrawerAdapter extends CarDrawerAdapter {
     29     private final CarDrawerActivity mActivity;
     30     private MediaItemsFetcher mCurrentFetcher;
     31 
     32     MediaDrawerAdapter(CarDrawerActivity activity, boolean useSmallLayout) {
     33         super(activity, true /* showDisabledListOnEmpty */, useSmallLayout);
     34         mActivity = activity;
     35     }
     36 
     37     /**
     38      * Switch the {@link MediaItemsFetcher} being used to fetch items. The new fetcher is kicked-off
     39      * and the drawer's content's will be updated to show newly loaded items. Any old fetcher is
     40      * cleaned up and released.
     41      *
     42      * @param fetcher New {@link MediaItemsFetcher} to use for display Drawer items.
     43      */
     44     void setFetcher(MediaItemsFetcher fetcher) {
     45         if (mCurrentFetcher != null) {
     46             mCurrentFetcher.cleanup();
     47         }
     48         mCurrentFetcher = fetcher;
     49         mCurrentFetcher.start(() -> {
     50             mActivity.showLoadingProgressBar(false);
     51             notifyDataSetChanged();
     52         });
     53         // Initially there will be no items and we don't want to show empty-list indicator briefly
     54         // until items are fetched.
     55         mActivity.showLoadingProgressBar(true);
     56     }
     57 
     58     @Override
     59     protected int getActualItemCount() {
     60         return mCurrentFetcher != null ? mCurrentFetcher.getItemCount() : 0;
     61     }
     62 
     63     @Override
     64     protected void populateViewHolder(DrawerItemViewHolder holder, int position) {
     65         if (mCurrentFetcher != null) {
     66             mCurrentFetcher.populateViewHolder(holder, position);
     67         }
     68     }
     69 
     70     @Override
     71     public void onItemClick(int position) {
     72         if (mCurrentFetcher != null) {
     73             mCurrentFetcher.onItemClick(position);
     74         }
     75     }
     76 }
     77