Home | History | Annotate | Download | only in applications
      1 /*
      2  * Copyright (C) 2010 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.android.providers.applications;
     18 
     19 import android.content.Context;
     20 import android.database.Cursor;
     21 import android.net.Uri;
     22 import android.provider.Applications;
     23 import android.text.TextUtils;
     24 import android.util.Log;
     25 import android.view.View;
     26 import android.widget.ImageView;
     27 import android.widget.ResourceCursorAdapter;
     28 import android.widget.TextView;
     29 
     30 public class ApplicationsAdapter extends ResourceCursorAdapter {
     31 
     32     private static final boolean DBG = false;
     33     private static final String TAG = "ApplicationsAdapter";
     34 
     35     public ApplicationsAdapter(Context context, Cursor c) {
     36         super(context, R.layout.application_list_item, c);
     37     }
     38 
     39     @Override
     40     public void bindView(View view, Context context, Cursor cursor) {
     41         ImageView icon1 = (ImageView) view.findViewById(R.id.icon1);
     42         TextView text1 = (TextView) view.findViewById(R.id.text1);
     43         Uri iconUri = getColumnUri(cursor, Applications.ApplicationColumns.ICON);
     44         String name = getColumnString(cursor, Applications.ApplicationColumns.NAME);
     45         if (DBG) Log.d(TAG, "name=" + name + ",icon=" + iconUri);
     46         icon1.setImageURI(iconUri);
     47         text1.setText(name);
     48     }
     49 
     50     public static Uri getColumnUri(Cursor cursor, String columnName) {
     51         String uriString = getColumnString(cursor, columnName);
     52         if (TextUtils.isEmpty(uriString)) return null;
     53         return Uri.parse(uriString);
     54     }
     55 
     56     public static String getColumnString(Cursor cursor, String columnName) {
     57         int col = cursor.getColumnIndex(columnName);
     58         return getStringOrNull(cursor, col);
     59     }
     60 
     61     private static String getStringOrNull(Cursor cursor, int col) {
     62         if (col < 0) return null;
     63         try {
     64             return cursor.getString(col);
     65         } catch (RuntimeException e) {
     66             Log.e(TAG, "Failed to get column " + col + " from cursor", e);
     67             return null;
     68         }
     69     }
     70 }
     71