Skip to content

Commit

Permalink
Basic menu
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyAdshead committed Feb 21, 2017
1 parent df77d13 commit 218fb20
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 11 deletions.
13 changes: 5 additions & 8 deletions app/src/main/java/com/baronkiko/launcherhijack/AccServ.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ public class AccServ extends AccessibilityService {

static final String TAG = "AccServ";

public Intent GetIntent()
public Intent GetDesiredIntent()
{
SharedPreferences settings = getSharedPreferences("LauncherHijack", 0);
String s = settings.getString("ChosenLauncher", null);
if (s == null)
s = "com.teslacoilsw.launcher";
SharedPreferences settings = getSharedPreferences("LauncherHijack", MODE_WORLD_READABLE);
String s = settings.getString("ChosenLauncher", "com.teslacoilsw.launcher");
return new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).setPackage(s).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
}

Expand All @@ -30,8 +28,7 @@ public void onAccessibilityEvent(AccessibilityEvent event) {
Log.wtf(TAG,e);
}
performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS);
startActivity(GetIntent());

startActivity(GetDesiredIntent());
}
}

Expand All @@ -57,7 +54,7 @@ protected void onServiceConnected() {
Log.wtf(TAG,e);
}
performGlobalAction(AccessibilityService.GLOBAL_ACTION_RECENTS);
startActivity(GetIntent());
startActivity(GetDesiredIntent());
}

}
72 changes: 72 additions & 0 deletions app/src/main/java/com/baronkiko/launcherhijack/AppInfoAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.baronkiko.launcherhijack;

import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AppInfoAdapter extends BaseAdapter {
private Context mContext;
private List<ApplicationInfo> mListAppInfo;
private PackageManager mPackManager;

public AppInfoAdapter(Context c, List<ApplicationInfo> list, PackageManager pm) {
mContext = c;
mListAppInfo = list;
mPackManager = pm;
}

@Override
public int getCount() {
return mListAppInfo.size();
}

@Override
public Object getItem(int position) {
return mListAppInfo.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get the selected entry
ApplicationInfo entry = mListAppInfo.get(position);

// reference to convertView
View v = convertView;

// inflate new layout if null
if(v == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
v = inflater.inflate(com.baronkiko.launcherhijack.R.layout.layout_appinfo, null);
}

// load controls from layout resources
ImageView ivAppIcon = (ImageView)v.findViewById(com.baronkiko.launcherhijack.R.id.ivIcon);
TextView tvAppName = (TextView)v.findViewById(com.baronkiko.launcherhijack.R.id.tvName);
TextView tvPkgName = (TextView)v.findViewById(com.baronkiko.launcherhijack.R.id.tvPack);

// set data to display
ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
tvAppName.setText(entry.loadLabel(mPackManager));
tvPkgName.setText(entry.packageName);

// return view
return v;
}




}
45 changes: 45 additions & 0 deletions app/src/main/java/com/baronkiko/launcherhijack/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,58 @@
package com.baronkiko.launcherhijack;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
private ListView mListAppInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.baronkiko.launcherhijack.R.layout.activity_main);

mListAppInfo = (ListView) findViewById(R.id.lvApps);
// create new adapter
AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
// set adapter to list view
mListAppInfo.setAdapter(adapter);
// implement event when an item on list view is selected
mListAppInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
// get the list adapter
AppInfoAdapter appInfoAdapter = (AppInfoAdapter) parent.getAdapter();
// get selected item on the list
ApplicationInfo appInfo = (ApplicationInfo) appInfoAdapter.getItem(pos);

// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences("LauncherHijack", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("ChosenLauncher", appInfo.packageName);
editor.commit(); // Commit the edits!


// Notify User
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Launcher Set");
alertDialog.setMessage("Your launcher has been set to " + appInfo.loadLabel(getPackageManager()) + " (" + appInfo.packageName + ")");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
});
}
}
51 changes: 51 additions & 0 deletions app/src/main/java/com/baronkiko/launcherhijack/Utilities.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.baronkiko.launcherhijack;

import java.util.List;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;

public class Utilities {

/*
* Get all installed application on mobile and return a list
* @param c Context of application
* @return list of installed applications
*/
public static List<ApplicationInfo> getInstalledApplication(Context c) {
return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
}

/*
* Launch an application
* @param c Context of application
* @param pm the related package manager of the context
* @param pkgName Name of the package to run
*/
public static boolean launchApp(Context c, PackageManager pm, String pkgName) {
// query the intent for lauching
Intent intent = pm.getLaunchIntentForPackage(pkgName);
// if intent is available
if(intent != null) {
try {
// launch application
c.startActivity(intent);
// if succeed
return true;

// if fail
} catch(ActivityNotFoundException ex) {
// quick message notification
Toast toast = Toast.makeText(c, "Application Not Found", Toast.LENGTH_LONG);
// display message
toast.show();
}
}
// by default, fail to launch
return false;
}
}
7 changes: 4 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.baronkiko.launcherhijack.MainActivity">

<TextView
android:layout_width="wrap_content"
<ListView
android:id="@+id/lvApps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World!" />
/>
</RelativeLayout>
44 changes: 44 additions & 0 deletions app/src/main/res/layout/layout_appinfo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="5dip"
>

<ImageView
android:id="@+id/ivIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="5dip"
android:scaleType="center"
/>

<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
>

<TextView
android:id="@+id/tvName"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>

<TextView
android:id="@+id/tvPack"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
/>

</LinearLayout>


</LinearLayout>

0 comments on commit 218fb20

Please sign in to comment.