Android Open Source - pinpoint-android Launcher Reader






From Project

Back to project page pinpoint-android.

License

The source code is released under:

MIT License

If you think the Android project pinpoint-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package co.islovely.pinpoint;
//from   w w w  .jav  a2s . c  o m
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import java.io.ByteArrayOutputStream;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

/* import android.database.DatabaseUtils; */

public class LauncherReader {
  public static List<LauncherItem> getAllItemsNotInDock(Context context) {
    List<LauncherItem> result = new ArrayList<LauncherItem>();

    for (LauncherItem launcherItem : LauncherReader.readDatabase(context)) {
      if (!launcherItem.isInDock() && launcherItem.isOnScreen()) {
        if (launcherItem.isShortcut() || launcherItem.isApplication() || launcherItem.isFolder()) {
          result.add(launcherItem);
        }
      }
    }

    return result;
  }

  public static List<LauncherItem> readDatabase(Context context) {
    List<LauncherItem> result = new ArrayList<LauncherItem>();

    PackageManager packageManager = context.getPackageManager();

    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_HOME);
    ResolveInfo launcherInfo = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

    String launcherPackageName = launcherInfo.activityInfo.packageName;
    String authority = null;

    for (PackageInfo packageInfo : packageManager.getInstalledPackages(PackageManager.GET_PROVIDERS)) {
      ProviderInfo[] providerInfos = packageInfo.providers;
      if (null != providerInfos) {
        for (ProviderInfo provider : providerInfos) {
          if (launcherPackageName.equals(provider.packageName)
              && null != provider.readPermission
              && provider.readPermission.contains("launcher.permission.READ_SETTINGS")) {
            authority = provider.authority;
            break;
          }
        }
      }
    }

    /* Pinpoint.log("Authority: " + authority); */

    if (null != authority) {
      String[] idealProjection = new String[] {
        "_id",
        "title",
        "intent",
        "container",
        "screen",
        "cellX",
        "cellY",
        "spanX",
        "spanY",
        "icon",
        "itemType"
      };
      String[] projection = null;

      String selection = null;
      String[] selectionArgs = null;
      String sortOrder = null;
      Cursor cursor = context.getContentResolver().query(Uri.parse("content://" + authority + "/favorites?notify=true"), projection, selection, selectionArgs, sortOrder);

      /* Pinpoint.log(DatabaseUtils.dumpCursorToString(cursor)); */

      cursor.moveToFirst();

      List<String> untrackedColumns = new ArrayList(Arrays.asList(cursor.getColumnNames()));
      untrackedColumns.removeAll(new ArrayList(Arrays.asList(idealProjection)));

      /* Pinpoint.log(cursor.getColumnCount() + " columns found in launcher.db, " + untrackedColumns.size() + " untracked."); */
      /* for (String column : untrackedColumns) { */
      /*   Pinpoint.log(column + ", " + cursor.getType(cursor.getColumnIndex(column))); */
      /* } */

      while (!cursor.isAfterLast()) {
        byte[] icon = cursor.getBlob(cursor.getColumnIndex("icon"));
        String intentValue = cursor.getString(cursor.getColumnIndex("intent"));

        // Shut up, launcher.db, I *will* get an icon!
        if (null == icon) {
          Drawable drawable = null;

          if (null == intentValue) {
            // folders do not have an associated intentthem, so we use an image
            // found in res/drawable
            drawable = context.getResources().getDrawable(R.drawable.folder);
          } else {
            // read icon straight from system for apps that do not have their
            // icon in launcher.db
            Intent intentForIcon = null;
            try {
              intentForIcon = Intent.parseUri(intentValue, 0);
            } catch (URISyntaxException exception) {
              Pinpoint.log("URISyntaxException");
            }

            if (null != intentForIcon) {
              try {
                drawable = packageManager.getActivityIcon(intentForIcon);
              } catch (NameNotFoundException exception) {
                Pinpoint.log("NameNotFoundException");
              }
            }
          }

          if (null != drawable) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            ((BitmapDrawable) drawable).getBitmap().compress(Bitmap.CompressFormat.PNG, 100, stream);
            icon = stream.toByteArray();
          }
        }

        result.add(new LauncherItem(
            cursor.getInt(cursor.getColumnIndex("_id")),
            cursor.getString(cursor.getColumnIndex("title")),
            intentValue,
            cursor.getInt(cursor.getColumnIndex("container")),
            cursor.getInt(cursor.getColumnIndex("screen")),
            cursor.getInt(cursor.getColumnIndex("cellX")),
            cursor.getInt(cursor.getColumnIndex("cellY")),
            cursor.getInt(cursor.getColumnIndex("spanX")),
            cursor.getInt(cursor.getColumnIndex("spanY")),
            cursor.getInt(cursor.getColumnIndex("itemType")),
            cursor.getInt(cursor.getColumnIndex("appWidgetId")),
            icon
        ));

        cursor.moveToNext();
      }
    }

    return result;
  }
}




Java Source Code List

co.islovely.pinpoint.ColumnsActivity.java
co.islovely.pinpoint.Configuration.java
co.islovely.pinpoint.DeviceMetrics.java
co.islovely.pinpoint.GridActivity.java
co.islovely.pinpoint.HomescreenAdapter.java
co.islovely.pinpoint.HomescreenSelectActivity.java
co.islovely.pinpoint.Homescreen.java
co.islovely.pinpoint.IntermissionActivity.java
co.islovely.pinpoint.LauncherItem.java
co.islovely.pinpoint.LauncherReader.java
co.islovely.pinpoint.LayoutConfigurationActivity.java
co.islovely.pinpoint.MainActivity.java
co.islovely.pinpoint.MyApplication.java
co.islovely.pinpoint.PinpointActivity.java
co.islovely.pinpoint.Pinpoint.java
co.islovely.pinpoint.QuadrantsActivity.java
co.islovely.pinpoint.RowsActivity.java
co.islovely.pinpoint.StatisticsActivity.java
co.islovely.pinpoint.TaskLogEntry.java
co.islovely.pinpoint.TaskLog.java
co.islovely.pinpoint.TaskManager.java
co.islovely.pinpoint.Task.java
co.islovely.pinpoint.User.java
util.Base64.java