Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

Commit

Permalink
Categorize method fixed
Browse files Browse the repository at this point in the history
PackagesCategories class was modified: now it loads the categories file only once. Also added more keywords.
  • Loading branch information
AlbertWDev committed Aug 25, 2016
1 parent 4f7e4b3 commit 73dffa0
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 120 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
*.iml
.idea
.gradle
/local.properties
/.idea/workspace.xml
Expand Down
24 changes: 0 additions & 24 deletions .idea/gradle.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

35 changes: 7 additions & 28 deletions app/src/main/java/com/launcher/silverfish/LauncherActivity.java
Expand Up @@ -28,6 +28,7 @@
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.DragEvent;
import android.view.KeyEvent;
import android.view.View;
Expand Down Expand Up @@ -108,48 +109,26 @@ private void createDefaultTabs() {
}
}

// Retrieve the default tab ID based on the English name
private int getCategoryId(String englishName) {
switch (englishName)
{
default: case "Other": return 1;
case "Phone": return 2;
case "Games": return 3;
case "Internet": return 4;
case "Media": return 5;
case "Accessories": return 6;
case "Settings": return 7;
}
}


// Auto sorts the applications in their corresponding tabs
private void autoSortApplications() {

// Set up both SQL helper and package manager
LauncherSQLiteHelper sql = new LauncherSQLiteHelper(this.getBaseContext());
PackageManager mPacMan = getApplicationContext().getPackageManager();

// Set MAIN and LAUNCHER filters, so we only get activities with that defined on their manifest
Intent i = new Intent(Intent.ACTION_MAIN, null);
i.addCategory(Intent.CATEGORY_LAUNCHER);

PackageManager mPacMan = getApplicationContext().getPackageManager();
// Get all activities that have those filters
List<ResolveInfo> availableActivities = mPacMan.queryIntentActivities(i, 0);


// Store here the packages and their categories IDs
// This will allow us to add all the apps at once instead opening the database over and over
Map<String, Integer> pkg_categoryId = new HashMap<>();

for (int j = 0; j < availableActivities.size(); j++) {
ResolveInfo ri = availableActivities.get(j);

String pkg = ri.activityInfo.packageName;
String category = PackagesCategories.getCategory(getApplicationContext(), pkg);
int categoryId = getCategoryId(category);

// Only add if not default
if (categoryId > 1) {
pkg_categoryId.put(pkg, categoryId);
}
}
HashMap<String, Integer> pkg_categoryId = PackagesCategories.setCategories(getApplicationContext(), availableActivities);

// Then add all the apps to their corresponding tabs at once
sql.addAppsToTab(pkg_categoryId);
Expand Down
183 changes: 130 additions & 53 deletions app/src/main/java/com/launcher/silverfish/utils/PackagesCategories.java
@@ -1,79 +1,156 @@
package com.launcher.silverfish.utils;

import android.content.Context;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.util.Log;

import com.launcher.silverfish.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;


public final class PackagesCategories {

public static final String DEFAULT_CATEGORY = "Other";
//region Helper methods

// Retrieve the default tab ID based on the English name
private static int getCategoryId(String englishName) {
switch (englishName)
{
default: case "Other": return 1;
case "Phone": return 2;
case "Games": return 3;
case "Internet": return 4;
case "Media": return 5;
case "Accessories": return 6;
case "Settings": return 7;
}
}

private static boolean containsKeyword(String str, String[] keywords) {
for (String keyword : keywords) {
if(str.contains(keyword)) return true;
}
return false;
}

//endregion

//region Get Categories

public static HashMap<String, String> getPredefinedCategories(Context ctx)
{
HashMap<String, String> predefCategories = new HashMap<>();

public static String getCategory(Context ctx, String pkg) {
InputStream inputStream = ctx.getResources().openRawResource(R.raw.package_category);
String line;
String[] lineSplit;

// Read the file containing a list in the form of package=category
try {
Resources res = ctx.getResources();
InputStream inputStream = res.openRawResource(R.raw.package_category);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));

// Read each line of the file until we find the right category
String line;
while ((line = reader.readLine()) != null) {
// The left side ([0]) of the string contains the package
if (pkg.equals(line.split("=")[0])) {

// Return the category, contained in the right side ([1])
return line.split("=")[1];
if (!line.isEmpty()){
lineSplit = line.split("=");
predefCategories.put(lineSplit[0], lineSplit[1]);
}
}
} catch (Exception e) { e.printStackTrace(); }


// Intelligent fallback: Try to guess the category
pkg = pkg.toLowerCase();
if (pkg.contains("conv") ||
pkg.contains("phone") ||
pkg.contains("call")) {
return "Phone";
}
if (pkg.contains("game") ||
pkg.contains("play")) {
return "Games";
}
if (pkg.contains("download") ||
pkg.contains("mail") ||
pkg.contains("vending")) {
return "Internet";
}
if (pkg.contains("pic") ||
pkg.contains("photo") ||
pkg.contains("cam") ||
pkg.contains("tube") ||
pkg.contains("radio") ||
pkg.contains("tv")) {
return "Media";
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if(inputStream != null) inputStream.close();
} catch (IOException e) { e.printStackTrace(); }
}
if (pkg.contains("calc") ||
pkg.contains("calendar") ||
pkg.contains("organize") ||
pkg.contains("clock") ||
pkg.contains("time")) {
return "Accessories";
}
if (pkg.contains("settings") ||
pkg.contains("config") ||
pkg.contains("keyboard") ||
pkg.contains("sync") ||
pkg.contains("backup")) {
return "Settings";

return predefCategories;
}

//endregion

//region Get Keywords

public static HashMap<String, String[]> getKeywords(Context ctx)
{
HashMap<String, String[]> keywordsDict = new HashMap<>();

keywordsDict.put("Phone", new String[]{"phone", "conv", "call", "sms", "mms", "contacts", "stk"}); // stk stands for "SIM Toolkit"
keywordsDict.put("Games", new String[]{"game", "play"});
keywordsDict.put("Internet", new String[]{"download", "mail", "vending", "browser", "maps", "twitter", "whatsapp", "outlook", "dropbox", "chrome", "drive"});
keywordsDict.put("Media", new String[]{"pic", "gallery", "photo", "cam", "tube", "radio", "tv", "voice", "video", "music"});
keywordsDict.put("Accessories", new String[]{"editor", "calc", "calendar", "organize", "clock", "time", "viewer", "file", "manager", "memo", "note"});
keywordsDict.put("Settings", new String[]{"settings", "config", "keyboard", "launcher", "sync", "backup"});


return keywordsDict;
}

//endregion

//region Set each package category

public static HashMap<String, Integer> setCategories(Context ctx, List<ResolveInfo> activities)
{
return setCategories(ctx, activities, getPredefinedCategories(ctx), getKeywords(ctx));
}

public static HashMap<String, Integer> setCategories(Context ctx, List<ResolveInfo> activities,
HashMap<String, String> categories,
HashMap<String, String[]> keywords)
{
HashMap<String, Integer> pkg_categoryId = new HashMap<>();
String pkg, category = "";
int categoryId;

for (int i = 0; i < activities.size(); i++) {
ResolveInfo ri = activities.get(i);
pkg = ri.activityInfo.packageName;


if (categories.containsKey(pkg)) {
category = categories.get(pkg);
categoryId = getCategoryId(category);

// Only add if not default
if (categoryId > 1) {
pkg_categoryId.put(pkg, categoryId);
}
}
// Intelligent fallback: Try to guess the category
else {
pkg = pkg.toLowerCase();
for (String key : keywords.keySet())
{
if (containsKeyword(pkg, keywords.get(key)))
{
if(pkg.contains("contacts")) Log.d("PACKAGES", "==== CONTACTS APP ====");
category = key;
pkg_categoryId.put(pkg, getCategoryId(key));
}
}
}
Log.d("PACKAGES",
"* Package: " + pkg +
"\n Activity: " + ri.activityInfo.name +
"\n Name: " + ri.loadLabel(ctx.getPackageManager()) +
"\n ResolvePackageName: " + ri.resolvePackageName +
"\n PackageName: " + ri.activityInfo.packageName +
"\n AppPackageName : " + ri.activityInfo.applicationInfo.packageName +
"\n AppLabel: " + ri.activityInfo.applicationInfo.loadLabel(ctx.getPackageManager()) +
"\n Category: " + category
);
}

// If we could not guess the category, return default
return DEFAULT_CATEGORY;
return pkg_categoryId;
}

//endregion

}
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.android.tools.build:gradle:2.1.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
#Thu Aug 25 15:18:23 CEST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip

0 comments on commit 73dffa0

Please sign in to comment.