1 /* 2 * Copyright (C) 2014 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.example.android.appusagestatistics; 18 19 import android.support.v7.widget.RecyclerView; 20 import android.view.LayoutInflater; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.widget.ImageView; 24 import android.widget.TextView; 25 26 import java.text.DateFormat; 27 import java.text.SimpleDateFormat; 28 import java.util.ArrayList; 29 import java.util.Date; 30 import java.util.List; 31 32 /** 33 * Provide views to RecyclerView with the directory entries. 34 */ 35 public class UsageListAdapter extends RecyclerView.Adapter<UsageListAdapter.ViewHolder> { 36 37 private List<CustomUsageStats> mCustomUsageStatsList = new ArrayList<>(); 38 private DateFormat mDateFormat = new SimpleDateFormat(); 39 40 /** 41 * Provide a reference to the type of views that you are using (custom ViewHolder) 42 */ 43 public static class ViewHolder extends RecyclerView.ViewHolder { 44 private final TextView mPackageName; 45 private final TextView mLastTimeUsed; 46 private final ImageView mAppIcon; 47 ViewHolder(View v)48 public ViewHolder(View v) { 49 super(v); 50 mPackageName = (TextView) v.findViewById(R.id.textview_package_name); 51 mLastTimeUsed = (TextView) v.findViewById(R.id.textview_last_time_used); 52 mAppIcon = (ImageView) v.findViewById(R.id.app_icon); 53 } 54 getLastTimeUsed()55 public TextView getLastTimeUsed() { 56 return mLastTimeUsed; 57 } 58 getPackageName()59 public TextView getPackageName() { 60 return mPackageName; 61 } 62 getAppIcon()63 public ImageView getAppIcon() { 64 return mAppIcon; 65 } 66 } 67 UsageListAdapter()68 public UsageListAdapter() { 69 } 70 71 @Override onCreateViewHolder(ViewGroup viewGroup, int viewType)72 public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 73 View v = LayoutInflater.from(viewGroup.getContext()) 74 .inflate(R.layout.usage_row, viewGroup, false); 75 return new ViewHolder(v); 76 } 77 78 @Override onBindViewHolder(ViewHolder viewHolder, final int position)79 public void onBindViewHolder(ViewHolder viewHolder, final int position) { 80 viewHolder.getPackageName().setText( 81 mCustomUsageStatsList.get(position).usageStats.getPackageName()); 82 long lastTimeUsed = mCustomUsageStatsList.get(position).usageStats.getLastTimeUsed(); 83 viewHolder.getLastTimeUsed().setText(mDateFormat.format(new Date(lastTimeUsed))); 84 viewHolder.getAppIcon().setImageDrawable(mCustomUsageStatsList.get(position).appIcon); 85 } 86 87 @Override getItemCount()88 public int getItemCount() { 89 return mCustomUsageStatsList.size(); 90 } 91 setCustomUsageStatsList(List<CustomUsageStats> customUsageStats)92 public void setCustomUsageStatsList(List<CustomUsageStats> customUsageStats) { 93 mCustomUsageStatsList = customUsageStats; 94 } 95 }