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
17package com.googlecode.android_scripting;
18
19import android.content.Context;
20import android.view.LayoutInflater;
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.BaseAdapter;
24import android.widget.ImageView;
25import android.widget.LinearLayout;
26import android.widget.TextView;
27
28import java.io.File;
29import java.util.List;
30
31public 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