forked from BaronKiko/LauncherHijack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df77d13
commit 218fb20
Showing
6 changed files
with
221 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
app/src/main/java/com/baronkiko/launcherhijack/AppInfoAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
app/src/main/java/com/baronkiko/launcherhijack/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
app/src/main/java/com/baronkiko/launcherhijack/Utilities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |