Home | History | Annotate | Download | only in car
      1 /*
      2  * Copyright 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.example.androidx.car;
     18 
     19 import android.app.ListActivity;
     20 import android.content.Intent;
     21 import android.content.pm.PackageManager;
     22 import android.content.pm.ResolveInfo;
     23 import android.os.Bundle;
     24 import android.text.TextUtils;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.BaseAdapter;
     28 import android.widget.ListView;
     29 import android.widget.TextView;
     30 
     31 import java.util.ArrayList;
     32 import java.util.Collections;
     33 import java.util.List;
     34 
     35 /**
     36  * Home activity for car support library samples.
     37  */
     38 public class SupportCarDemoActivity extends ListActivity {
     39     @Override
     40     protected void onCreate(Bundle savedInstanceState) {
     41         super.onCreate(savedInstanceState);
     42 
     43         setListAdapter(new SampleAdapter(querySampleActivities()));
     44     }
     45 
     46     @Override
     47     protected void onListItemClick(ListView lv, View v, int pos, long id) {
     48         SampleInfo info = (SampleInfo) getListAdapter().getItem(pos);
     49         startActivity(info.mIntent);
     50     }
     51 
     52     protected List<SampleInfo> querySampleActivities() {
     53         Intent intent = new Intent(Intent.ACTION_MAIN, null);
     54         intent.setPackage(getPackageName());
     55         intent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
     56 
     57         PackageManager pm = getPackageManager();
     58         List<ResolveInfo> infos = pm.queryIntentActivities(intent, 0);
     59 
     60         ArrayList<SampleInfo> samples = new ArrayList<>();
     61 
     62         final int count = infos.size();
     63         for (int i = 0; i < count; i++) {
     64             final ResolveInfo info = infos.get(i);
     65             final CharSequence labelSeq = info.loadLabel(pm);
     66             String label = TextUtils.isEmpty(labelSeq)
     67                     ? info.activityInfo.name
     68                     : labelSeq.toString();
     69 
     70             Intent target = new Intent();
     71             target.setClassName(info.activityInfo.applicationInfo.packageName,
     72                     info.activityInfo.name);
     73             SampleInfo sample = new SampleInfo(label, target);
     74             samples.add(sample);
     75         }
     76 
     77         Collections.sort(samples);
     78 
     79         return samples;
     80     }
     81 
     82     static class SampleInfo implements Comparable<SampleInfo> {
     83         String mName;
     84         Intent mIntent;
     85 
     86         SampleInfo(String name, Intent intent) {
     87             this.mName = name;
     88             this.mIntent = intent;
     89         }
     90 
     91         @Override
     92         public int compareTo(SampleInfo o) {
     93             return this.mName.compareTo(o.mName);
     94         }
     95     }
     96 
     97     class SampleAdapter extends BaseAdapter {
     98         private List<SampleInfo> mItems;
     99 
    100         SampleAdapter(List<SampleInfo> items) {
    101             mItems = items;
    102         }
    103 
    104         @Override
    105         public int getCount() {
    106             return mItems.size();
    107         }
    108 
    109         @Override
    110         public Object getItem(int position) {
    111             return mItems.get(position);
    112         }
    113 
    114         @Override
    115         public long getItemId(int position) {
    116             return position;
    117         }
    118 
    119         @Override
    120         public View getView(int position, View convertView, ViewGroup parent) {
    121             if (convertView == null) {
    122                 convertView = getLayoutInflater().inflate(R.layout.main_list_item, parent, false);
    123             }
    124             TextView tv = convertView.findViewById(R.id.title);
    125             tv.setText(mItems.get(position).mName);
    126             return convertView;
    127         }
    128 
    129     }
    130 }
    131