Browse files

Basic functionality works. Nothing fancy, but that's okay.

  • Loading branch information...
1 parent 9ba1305 commit 6bfbfd0dad7108edb7899293825dcfe2eeabece0 @dbjorge committed May 31, 2011
Showing with 133 additions and 11 deletions.
  1. +37 −8 res/layout/main.xml
  2. +2 −1 res/values/strings.xml
  3. +94 −2 src/com/brosmike/airpushdetector/AirPushDetectorActivity.java
View
45 res/layout/main.xml
@@ -1,12 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
-<TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
-</LinearLayout>
+ <LinearLayout
+ android:id="@+id/button_bar"
+ style="@android:style/ButtonBar"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:layout_alignParentBottom="true"
+ >
+ <Button
+ android:id="@+id/refresh_button"
+ android:text="@string/refresh"
+ android:onClick="onRefreshButtonClick"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center"
+ />
+ </LinearLayout>
+ <LinearLayout
+ android:id="@+id/list_container"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:layout_alignParentTop="true"
+ android:layout_above="@id/button_bar"
+ >
+ <ListView
+ android:id="@android:id/list"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ />
+ <TextView
+ android:id="@android:id/empty"
+ android:text="@string/no_airpush_apps"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ />
+ </LinearLayout>
+</RelativeLayout>
View
3 res/values/strings.xml
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
- <string name="hello">Hello World, AirPushDetectorActivity!</string>
<string name="app_name">AirPush Detector</string>
+ <string name="refresh">Refresh</string>
+ <string name="no_airpush_apps">No installed AirPush apps detected!</string>
</resources>
View
96 src/com/brosmike/airpushdetector/AirPushDetectorActivity.java
@@ -1,13 +1,105 @@
package com.brosmike.airpushdetector;
-import android.app.Activity;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import android.app.ListActivity;
+import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.net.Uri;
import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.widget.ListView;
+import android.widget.SimpleAdapter;
+
+public class AirPushDetectorActivity extends ListActivity {
+ private static final String TAG = "AirPushDetector";
+
+ /** The list of packages currently being displayed. Used in onListItemClick listener */
+ private List<PackageInfo> mPackages;
-public class AirPushDetectorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
+
+ @Override
+ public void onStart() {
+ super.onStart();
+ populateView();
+ }
+
+ @Override
+ public void onListItemClick(ListView listView, View view, int position, long id) {
+ PackageInfo pkg = mPackages.get(position);
+ Intent i = new Intent(Intent.ACTION_DELETE);
+ i.setData(Uri.parse("package:"+pkg.packageName));
+ startActivity(i);
+ }
+
+ public void onRefreshButtonClick(View button) {
+ populateView();
+ }
+
+ /** Populates the main view according to installed AirPush packages */
+ private void populateView() {
+ List<PackageInfo> airPushPackages = getAirPushPackages();
+ PackageManager pm = getPackageManager();
+
+ List<Map<String, String>> data = new ArrayList<Map<String, String>>(airPushPackages.size());
+ for(PackageInfo pkg : airPushPackages) {
+ Map<String, String> attrs = new HashMap<String, String>();
+ attrs.put("App Name", pm.getApplicationLabel(pkg.applicationInfo).toString());
+ attrs.put("Package Name", pkg.packageName);
+ data.add(attrs);
+ }
+
+ String[] from = new String[] {
+ "App Name",
+ "Package Name"
+ };
+ int[] to = new int[] {
+ android.R.id.text1,
+ android.R.id.text2
+ };
+ SimpleAdapter adapter = new SimpleAdapter(
+ this, data, android.R.layout.two_line_list_item, from, to);
+
+ setListAdapter(adapter);
+ mPackages = airPushPackages;
+ }
+
+ /** Finds all installed packages that look like they include AirPush */
+ private List<PackageInfo> getAirPushPackages() {
+ List<PackageInfo> airPushPackages = new ArrayList<PackageInfo>();
+
+ PackageManager pm = getPackageManager();
+ //It'd be simpler to just use pm.getInstalledPackages here, but apparently it's broken
+ List<ApplicationInfo> appInfos = pm.getInstalledApplications(0);
+ for(ApplicationInfo appInfo : appInfos) {
+ try {
+ PackageInfo pkgInfo = pm.getPackageInfo(appInfo.packageName, PackageManager.GET_ACTIVITIES);
+ if(pkgInfo.activities == null) continue;
+ for(ActivityInfo activity : pkgInfo.activities) {
+ if(activity.name.startsWith("com.airpush.")) {
+ airPushPackages.add(pkgInfo);
+ break;
+ }
+ }
+ } catch (NameNotFoundException e) {
+ Log.e(TAG, "Managed to not find a package we know about");
+ }
+ }
+
+ return airPushPackages;
+ }
}

0 comments on commit 6bfbfd0

Please sign in to comment.