Home | History | Annotate | Download | only in systemupdater
      1 /*
      2  * Copyright (C) 2015 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.systemupdater;
     17 
     18 import android.content.Context;
     19 import android.graphics.Color;
     20 import android.view.LayoutInflater;
     21 import android.view.View;
     22 import android.view.ViewGroup;
     23 import android.widget.ArrayAdapter;
     24 import android.widget.TextView;
     25 
     26 import java.io.File;
     27 
     28 
     29 public class FileAdapter extends ArrayAdapter<File> {
     30     private final Context mContext;
     31     private File[] mLocations;
     32     private final int mLayoutResourceId;
     33 
     34     public FileAdapter(Context c, int layoutResourceId, File[] locations) {
     35         super(c, layoutResourceId, locations);
     36         mContext = c;
     37         this.mLayoutResourceId = layoutResourceId;
     38         this.mLocations = locations;
     39     }
     40     @Override
     41     public View getView(int position, View convertView, ViewGroup parent) {
     42         ViewHolder vh = new ViewHolder();
     43         if (convertView == null) {
     44             LayoutInflater inflater = LayoutInflater.from(mContext);
     45             convertView = inflater.inflate(mLayoutResourceId, parent, false);
     46             vh.textView = (TextView) convertView.findViewById(R.id.text);
     47             vh.descriptionView = (TextView) convertView.findViewById(R.id.description);
     48             convertView.setTag(vh);
     49         } else {
     50             vh = (ViewHolder) convertView.getTag();
     51         }
     52         if (mLocations[position] != null) {
     53             vh.textView.setText(mLocations[position].getAbsolutePath());
     54             if (mLocations[position].getAbsolutePath().endsWith(".zip")
     55                     || mLocations[position].isDirectory()) {
     56                 vh.textView.setTextColor(Color.GREEN);
     57             } else {
     58                 vh.textView.setTextColor(Color.GRAY);
     59             }
     60         }
     61         return convertView;
     62     }
     63 
     64     @Override
     65     public int getCount() {
     66         return mLocations.length;
     67     }
     68 
     69     public void setLocations(File[] locations) {
     70         mLocations = locations;
     71         notifyDataSetChanged();
     72     }
     73 
     74     static class ViewHolder {
     75         TextView textView;
     76         TextView descriptionView;
     77     }
     78 }
     79