Home | History | Annotate | Download | only in applications
      1 /*
      2  * Copyright (C) 2009 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.app.ListActivity;
     20 import android.app.SearchManager;
     21 import android.content.ActivityNotFoundException;
     22 import android.content.ComponentName;
     23 import android.content.Intent;
     24 import android.database.Cursor;
     25 import android.net.Uri;
     26 import android.os.Bundle;
     27 import android.provider.Applications;
     28 import android.util.Log;
     29 import android.view.View;
     30 import android.widget.ListView;
     31 
     32 /**
     33  * This class is purely here to get launch intents.
     34  */
     35 public class ApplicationLauncher extends ListActivity {
     36 
     37     private static final String TAG = "ApplicationLauncher";
     38 
     39     private Cursor mCursor;
     40 
     41     @Override
     42     protected void onCreate(Bundle savedInstanceState) {
     43         super.onCreate(savedInstanceState);
     44         Intent intent = getIntent();
     45         if (intent == null) {
     46             finish();
     47             return;
     48         }
     49         String action = intent.getAction();
     50         if (Intent.ACTION_MAIN.equals(action)) {
     51             Uri contentUri = intent.getData();
     52             launchApplication(contentUri);
     53             finish();
     54         } else if (Intent.ACTION_SEARCH.equals(action)) {
     55             String query = intent.getStringExtra(SearchManager.QUERY);
     56             showSearchResults(query);
     57         }
     58     }
     59 
     60     private void showSearchResults(String query) {
     61         setTitle(query);
     62 
     63         mCursor = Applications.search(getContentResolver(), query);
     64         startManagingCursor(mCursor);
     65 
     66         ApplicationsAdapter adapter = new ApplicationsAdapter(this, mCursor);
     67         setListAdapter(adapter);
     68     }
     69 
     70     @Override
     71     protected void onListItemClick(ListView l, View v, int position, long id) {
     72         if (mCursor == null) {
     73             Log.e(TAG, "Got click on position " + position + " but there is no cursor");
     74             return;
     75         }
     76         if (mCursor.isClosed()) {
     77             Log.e(TAG, "Got click on position " + position + " but the cursor is closed");
     78             return;
     79         }
     80         if (!mCursor.moveToPosition(position)) {
     81             Log.e(TAG, "Failed to move to position " + position);
     82             return;
     83         }
     84         Uri uri = ApplicationsAdapter.getColumnUri(mCursor, Applications.ApplicationColumns.URI);
     85         launchApplication(uri);
     86     }
     87 
     88     private void launchApplication(Uri uri) {
     89         ComponentName componentName = Applications.uriToComponentName(uri);
     90         Log.i(TAG, "Launching " + componentName);
     91         if (componentName != null) {
     92             Intent launchIntent = new Intent(Intent.ACTION_MAIN);
     93             launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
     94                     Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
     95             launchIntent.setComponent(componentName);
     96             try {
     97                 startActivity(launchIntent);
     98             } catch (ActivityNotFoundException ex) {
     99                 Log.w(TAG, "Activity not found: " + componentName);
    100             }
    101         }
    102     }
    103 }
    104