Skip to content

Commit

Permalink
reduce the need of Context references by using the global app context
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurzaczek committed May 13, 2014
1 parent e09889d commit 5349aa3
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/net/zaczek/launcherforblind/AppsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected ListEntry[] getList() {
if (TextUtils.isEmpty(lb)) {
lb = info.activityInfo.name.toString();
}
result.add(new AppListEntry(lb, this, info));
result.add(new AppListEntry(lb, info));
}
}

Expand Down
37 changes: 21 additions & 16 deletions src/net/zaczek/launcherforblind/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,32 @@ public static void dial(Context ctx, String number) {
}
}

/*
* Returns the contact name to a given number or returns the number if no name was found
/*
* Returns the contact name to a given number or returns the number if no
* name was found
*/
public static String getContactName(Context ctx, String phoneNumber) {
ContentResolver cr = ctx.getContentResolver();
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
final ContentResolver cr = ctx.getContentResolver();
final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri,
new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
if (cursor == null) {
return null;
}
Cursor cursor = null;
String contactName = phoneNumber;
if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor
.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
try {
cursor = cr.query(uri, new String[] { PhoneLookup.DISPLAY_NAME },
null, null, null);
if (cursor == null) {
return null;
}

if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor
.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return contactName;
}

}
10 changes: 5 additions & 5 deletions src/net/zaczek/launcherforblind/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public void onCreate(Bundle savedInstanceState) {
protected ListEntry[] getList() {
return new ListEntry[] {
new StaticListEntry("Start Schirm"),
new NavigatorListEntry(getString(R.string.phonebook), this,
new NavigatorListEntry(getString(R.string.phonebook),
PhoneBookActivity.class),
new NavigatorListEntry(getString(R.string.dialer), this,
new NavigatorListEntry(getString(R.string.dialer),
DialerActivity.class),
new NavigatorListEntry(getString(R.string.missedcalls), this,
new NavigatorListEntry(getString(R.string.missedcalls),
MissedCallsActivity.class),
new NavigatorListEntry(getString(R.string.sms), this,
new NavigatorListEntry(getString(R.string.sms),
SMSActivity.class),
new NavigatorListEntry(getString(R.string.apps), this,
new NavigatorListEntry(getString(R.string.apps),
AppsActivity.class),
new TimeListEntry(getString(R.string.currenttime),
getString(R.string.time_format)), };
Expand Down
14 changes: 11 additions & 3 deletions src/net/zaczek/launcherforblind/MyApplication.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package net.zaczek.launcherforblind;

import android.app.Application;
import android.content.Context;

public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
}
private static Context context;

public void onCreate(){
super.onCreate();
MyApplication.context = getApplicationContext();
}

public static Context getAppContext() {
return MyApplication.context;
}
}
23 changes: 21 additions & 2 deletions src/net/zaczek/launcherforblind/SMSActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
public class SMSActivity extends AbstractCursorActivity {
private static final String TAG = "launcherforblind";

private static final String[] PROJECTION = new String[] { "address", "date", "body" };
private static final String[] PROJECTION = new String[] { "address",
"date", "body" };

private TextView txtMain;
private boolean confirmed = false;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -38,11 +40,28 @@ protected ListEntry getListEntry(Cursor c) {
long date = c.getLong(1);
String strDate = DateUtils.formatDateTime(this, date,
DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME);
return new SMSListEntry(this, c.getString(0), strDate, c.getString(2));
return new SMSListEntry(c.getString(0), strDate, c.getString(2));
}

@Override
protected void giveFeedback(String label) {
txtMain.setText(label);
confirmed = false;
}

@Override
protected void onExecute() {
super.onExecute();
final SMSListEntry current = (SMSListEntry) getCurrentListEntry();
if (current == null)
return;

if (!confirmed) {
Helper.confirmDial(this, current.getLabel());
confirmed = true;
} else {
// actually dial
Helper.dial(this, current.getNumber());
}
}
}
16 changes: 9 additions & 7 deletions src/net/zaczek/launcherforblind/listentries/AppListEntry.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package net.zaczek.launcherforblind.listentries;

import net.zaczek.launcherforblind.MyApplication;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;

public class AppListEntry extends AbstractListEntry implements Comparable<AppListEntry> {
public class AppListEntry extends AbstractListEntry implements
Comparable<AppListEntry> {
private ResolveInfo mInfo;
private Context mCtx;

public AppListEntry(String label, Context ctx, ResolveInfo info) {
public AppListEntry(String label, ResolveInfo info) {
super(label);
mCtx = ctx;
mInfo = info;
}

@Override
public void onSelected() {
final Intent i = mCtx.getPackageManager().getLaunchIntentForPackage(
final Context ctx = MyApplication.getAppContext();
final Intent i = ctx.getPackageManager().getLaunchIntentForPackage(
mInfo.activityInfo.packageName);
mCtx.startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(i);
}

@Override
public int compareTo(AppListEntry another) {
public int compareTo(AppListEntry another) {
return getLabel().compareTo(another.getLabel());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.zaczek.launcherforblind.listentries;

import net.zaczek.launcherforblind.MyApplication;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
Expand All @@ -9,21 +10,22 @@ public class NavigatorListEntry extends AbstractListEntry {

@SuppressWarnings("rawtypes")
private Class mActivityClass;
private Context mCtx;

@SuppressWarnings("rawtypes")
public NavigatorListEntry(String label, Context ctx, Class activityClass) {
public NavigatorListEntry(String label, Class activityClass) {
super(label);

mCtx = ctx;
mActivityClass = activityClass;
}

@Override
public void onSelected() {
if (mActivityClass != null) {
final Context ctx = MyApplication.getAppContext();
Log.i(TAG, "Starting activity " + mActivityClass);
mCtx.startActivity(new Intent(mCtx, mActivityClass));
Intent i = new Intent(ctx, mActivityClass);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(i);
} else {
Log.w(TAG, "Unable to navigate - no class provided");
}
Expand Down
13 changes: 6 additions & 7 deletions src/net/zaczek/launcherforblind/listentries/SMSListEntry.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package net.zaczek.launcherforblind.listentries;

import net.zaczek.launcherforblind.Helper;
import android.content.Context;
import net.zaczek.launcherforblind.MyApplication;

public class SMSListEntry extends AbstractListEntry {
String mNumber;
String mDate;
String mMessage;
Context mCtx;

public SMSListEntry(Context ctx, String number, String date, String msg) {

public SMSListEntry(String number, String date, String msg) {
super(date);
mCtx = ctx;
mNumber = number;
mDate = date;
mMessage = msg;
}

@Override
public String getLabelToSay() {
return Helper.getContactName(mCtx, mNumber) + ", " + mDate + ".\n" + mMessage;
return Helper.getContactName(MyApplication.getAppContext(), mNumber)
+ ", " + mDate + ".\n" + mMessage;
}

public String getNumber() {
Expand Down

0 comments on commit 5349aa3

Please sign in to comment.