Home | History | Annotate | Download | only in android_scripting
      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 
     17 package com.googlecode.android_scripting;
     18 
     19 import android.content.Context;
     20 import android.view.LayoutInflater;
     21 import android.view.View;
     22 import android.view.ViewGroup;
     23 import android.widget.BaseAdapter;
     24 import android.widget.ImageView;
     25 import android.widget.LinearLayout;
     26 import android.widget.TextView;
     27 
     28 import java.io.File;
     29 import java.util.List;
     30 
     31 public abstract class ScriptListAdapter extends BaseAdapter {
     32 
     33   protected final Context mContext;
     34   protected final LayoutInflater mInflater;
     35 
     36   public ScriptListAdapter(Context context) {
     37     mContext = context;
     38     mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     39   }
     40 
     41   @Override
     42   public int getCount() {
     43     return getScriptList().size();
     44   }
     45 
     46   @Override
     47   public Object getItem(int position) {
     48     return getScriptList().get(position);
     49   }
     50 
     51   @Override
     52   public long getItemId(int position) {
     53     return position;
     54   }
     55 
     56   @Override
     57   public View getView(int position, View convertView, ViewGroup parent) {
     58     LinearLayout container;
     59     File script = getScriptList().get(position);
     60 
     61     if (convertView == null) {
     62       container = (LinearLayout) mInflater.inflate(R.layout.list_item, null);
     63     } else {
     64       container = (LinearLayout) convertView;
     65     }
     66 
     67     ImageView icon = (ImageView) container.findViewById(R.id.list_item_icon);
     68     int resourceId;
     69     if (script.isDirectory()) {
     70       resourceId = R.drawable.folder;
     71     } else {
     72       resourceId = FeaturedInterpreters.getInterpreterIcon(mContext, script.getName());
     73       if (resourceId == 0) {
     74         resourceId = R.drawable.sl4a_logo_32;
     75       }
     76     }
     77     icon.setImageResource(resourceId);
     78 
     79     TextView text = (TextView) container.findViewById(R.id.list_item_title);
     80     text.setText(getScriptList().get(position).getName());
     81     return container;
     82   }
     83 
     84   protected abstract List<File> getScriptList();
     85 }
     86