Succincting multiple switch/case statements - android

Alright, I'm trying to succinct some code in my app. It works fine, I'm just a bit OCD and want to keep improving performance.
Here's what the code in question looks like now:
switch(ressound){
case R.id.button40:
ressound = R.raw.sound40;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote40));
break;
}
switch(ressound){
case R.id.button900:
ressound = R.raw.sound900;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote900));
break;
}
switch(ressound){
case R.id.button901:
ressound = R.raw.sound901;
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(R.string.quote901));
break;
}
It's a soundboard app, and this is regarding the save as feature in it. Is there any way to succinct these multiple statements (some screens have 40+ sounds)? Using a loop looks like an obvious choice, however after looking around, the case statement apparently has to be static and not a variable.
EDIT: Forgot to include the actual function header:
public boolean function1(int ressound){
String soundname = "";

int quoteval=0;
switch(ressound){
case R.id.button40:
ressound = R.raw.sound40;
quoteval =R.string.quote40;
break;
case R.id.button900:
ressound = R.raw.sound900;
quoteval =R.string.quote900;
break;
case R.id.button901:
ressound = R.raw.sound901;
quoteval =R.string.quote901;
break;
}
soundname = (this.getString(R.string.app_name)) + " - " + (this.getString(quoteval));

There is a way you can use a loop and eliminate the switch-case statement. Use the Resources#getIdentifier method. Assuming you name your resources with sequential numbers ("sound_filename_1", "sound_filename_2", etc) you can write some code like this:
private static ArrayList<Integer> findSoundResourceIds(Resources res) {
ArrayList<Integer> resIds = new ArrayList<Integer>();
int i = 1;
do {
int resId = res.getIdentifier("sound_filename_"+i, "raw", getPackage().getName());
if (resId == 0) {
break;
}
resIds.add(resId);
i++;
} while (true);
return resIds;
}

Related

how much is the number of SMS sending with SMSManager?

I would like to know how much is the sending of an SMS via SMSManager? let me explain:
I have a list of several people and I make a FOREACH loop on their phone number.
The list contains more than 200 numbers
Thanks
after going through the Android sources for all the APIs supported by my app I cam up with the following code
`private void setSmsDefaultLimitations(){
int apiLevel = Build.VERSION.SDK_INT;
String versionRelease = Build.VERSION.RELEASE;
switch(apiLevel){
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
sMaxAllowed = 100;
sCheckPeriod = 3600000;
break;
case 16:
sMaxAllowed = 30;
sCheckPeriod = 1800000;
break;
case 17:
sMaxAllowed = 30;
if(versionRelease.contains("4.2.2")){
sCheckPeriod = 60000;
}else {
sCheckPeriod = 1800000;
}
break;
case 18:
sMaxAllowed = 30;
sCheckPeriod = 60000;
break;
default:
sMaxAllowed = 30;
sCheckPeriod = 1800000;
break;
}
sMaxAllowed = sMaxAllowed - 2; //This is to give us a little buffer to be extra safe (like a condom ;)
Log.d(TAG, "maxAllowed = "+sMaxAllowed+"; checkPeriod = "+(sCheckPeriod/60000) + " minutes");
}`

Unreachable code after switch statement

public Cursor query(Uri paramUri, String[] paramArrayOfString1, String paramString1,String[] paramArrayOfString2, String paramString2)
{
SQLiteQueryBuilder localSQLiteQueryBuilder = new SQLiteQueryBuilder();
if (paramUri.getPathSegments().size() == 1);
for (StringBuilder localStringBuilder = null; ; localStringBuilder = new StringBuilder(100))
switch (sURIMatcher.match(paramUri))
{
case 0:
case 1:
case 2:
case 3:
default:
throw new IllegalArgumentException("Unknown URI " + paramUri);
}
localSQLiteQueryBuilder.setTables("category");//unreachable code
while (true)
{
Cursor localCursor = localSQLiteQueryBuilder.query(mOpenHelper.getReadableDatabase(), paramArrayOfString1, paramString1, paramArrayOfString2, null, null, paramString2);
localCursor.setNotificationUri(contentResolver, paramUri);
return localCursor;
localSQLiteQueryBuilder.setTables("shop,category");
localSQLiteQueryBuilder.appendWhere("shop_category_id=category._id");
continue;
localSQLiteQueryBuilder.setTables("shop,category");
StringBuilder localStringBuilder;
localStringBuilder.append("shop_category_id=category._id");
localStringBuilder.append(" AND ");
localStringBuilder.append("_id");
localStringBuilder.append('=');
localStringBuilder.append((String)paramUri.getPathSegments().get(1));
localSQLiteQueryBuilder.appendWhere(localStringBuilder.toString());
continue;
localSQLiteQueryBuilder.setTables("shop,category");
localSQLiteQueryBuilder.setDistinct(true);
localStringBuilder.append("shop_category_id=category._id");
localStringBuilder.append(" AND ");
localStringBuilder.append("shop_category_id");
localStringBuilder.append('=');
localStringBuilder.append((String)paramUri.getPathSegments().get(1));
localSQLiteQueryBuilder.appendWhere(localStringBuilder.toString());
paramString2 = "shop._id";
}
}
I get unreachable code error after switch statement and I can't figure it out how to solve it.I tried to delete that line but if I do I get a lot of errors.My code is above.Can anyone help me?Thanks in advance.
The code is really unreachable:
All the cases are fall-through (they have no break statement, so all cases after a match will execute) and end in the default case which throws an Exception. This means the code after throwing the Exception will never be executed.
Perhaps what you meant to do was rahter sth like this:
switch (sURIMatcher.match(paramUri)){
case 0:
// do something
break;
case 1:
// do something
break;
case 2:
// do something
break;
case 3:
// do something
break;
default:
throw new IllegalArgumentException("Unknown URI " + paramUri);
}
I guess it is due to your bad switch...you must use break;
switch (sURIMatcher.match(paramUri))
{
case 0:
//your code
break;
case 1:
//your code
break;
case 2:
//your code
break;
case 3:
//your code
break;
default:
//your code
break;
}

switch\case not working as expected

My Log line says animalclass is 4 but instead of going to "case 4" it returns default. It works with case 2 though. How is this possible? Thanks in advance.
public int gettile(int animalclass) {
Log.e("gettile", "animalclass = " + animalclass);
switch (animalclass) {
case 1: //
tile=R.drawable.picnictile;
break;
case 2: //
tile=R.drawable.picnictile;
break;
case 3: //
tile=R.drawable.picnictile;
case 4: //
tile=R.drawable.picnictile;
case 5: //
tile=R.drawable.face;
default:
Log.e("gettile", "failed!!!!!!!!!! = " + animalclass);
tile=R.drawable.rainbowtile;
break;
}
Log.e("gettile", "returning = " + tile);
return tile;
}
you need to use break; to stop other case execution because without break it will execute the correct case block statements and also "default" code block
Try adding the Break statemenet after all cases.
case 3: //
tile=R.drawable.picnictile;
break;
case 4: //
tile=R.drawable.picnictile;
break;
case 5: //
tile=R.drawable.face;
break;
If you dont break it after the "thing" the case should do, the switch does not work correct.
You forgot to add a break keyword before the default keyword.
Try this one:
public int gettile(int animalclass) {
Log.e("gettile", "animalclass = " + animalclass);
switch (animalclass) {
case 1: //
tile=R.drawable.picnictile;
break;
case 2: //
tile=R.drawable.picnictile;
break;
case 3: //
tile=R.drawable.picnictile;
break;
case 4: //
tile=R.drawable.picnictile;
break;
case 5: //
tile=R.drawable.face;
break;
default:
Log.e("gettile", "failed!!!!!!!!!! = " + animalclass);
tile=R.drawable.rainbowtile;
break;
}
Log.e("gettile", "returning = " + tile);
return tile;
}
Add break; keyword after each case, otherwise switch will execute default statement also and result will be unexpected.

Android Calculator getting Grand Total (GT)

As I am implementing an Android Calculator, I've come across another requirement of client.
I need to implement the feature in calculator that does the following:
2x3 =6, 2x2 = 4, 2x5 = 10.
Hit GT and will bring up 20(adds up all totals)
I've taken Buttons to perform this operations
Whatever button is pressed, I am taking its KeyCode to detect its number.
Then operator is taken as int operator which can take one of four values. i.e. 1=Add,2=Subtract, and all that.
here is a sample code to do such mathematical operation:
private void handleEquals(int newOperator) {
if (hasChanged) {
switch (operator) {
case 1:
num = num + Double.parseDouble(txtCalc.getText().toString());
break;
case 2:
num = num - Double.parseDouble(txtCalc.getText().toString());
break;
case 3:
num = num * Double.parseDouble(txtCalc.getText().toString());
String strNum = null;
strNum = Double.toString(num);
if(strNum.contains("E")){
strNum = strNum.substring(0, 6) + strNum.substring(strNum.indexOf("E"));
}
Log.i("MULTIPLICATION","Checking Precision");
System.out.println("New StrNum is: " + strNum);
num = Double.valueOf(strNum);
break;
case 4:
num = num / Double.parseDouble(txtCalc.getText().toString());
break;
}
Any suggestions or coding snippets would be greatly appreciated.
Thanks

Android TelephonyManager.getNetworkType() returned constant values in bearer speed order?

TelephonyManager.getNetworkType() returns one of the constant values.
It appears that the constant values have an integer order, by possible bearer link speed.
I know using constant values used in the following manner is generally bad,
however could one use this to determine a basic cutoff for application functionality and have it work between API levels? (in API-v1 there was nothing above 0x03)
if( telephonyManager.getNetworkType() > TelephonyManager.NETWORK_TYPE_EDGE )
{
return "3G! party on!";
}
else if( telephonyManager.getNetworkType() > TelephonyManager.NETWORK_TYPE_UNKNOWN )
{
return "2G, OK. just don't go nuts!";
}
else
{
return "No data sorry"
}
You cannot assume they are in order because they are not. For example, LTE is 13 while HSPAP (HSPA+) is 15. Those are not in order. I wrote a "speed ranking" piece of code, which assignes each network type its own speed rank
public static int getNetTypeSpeedRank(int t) {
switch (t) {
case -1:
t = -1;
case ContextManager.MDM_NETWORK_TYPE_UNKNOWN:
t = 0;
break;
case ContextManager.MDM_NETWORK_TYPE_IDEN:
t = 1;
break;
case ContextManager.MDM_NETWORK_TYPE_GPRS:
t = 2;
break;
case ContextManager.MDM_NETWORK_TYPE_EDGE:
t = 3;
break;
case ContextManager.MDM_NETWORK_TYPE_UMTS:
t = 4;
break;
case ContextManager.MDM_NETWORK_TYPE_CDMA:
t = 5;
break;
case ContextManager.MDM_NETWORK_TYPE_1xRTT:
t = 6;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_0:
t = 7;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_A:
t = 8;
break;
case ContextManager.MDM_NETWORK_TYPE_EVDO_B:
t = 9;
break;
case ContextManager.MDM_NETWORK_TYPE_HSDPA:
t = 10;
break;
case ContextManager.MDM_NETWORK_TYPE_HSUPA:
t = 11;
break;
case ContextManager.MDM_NETWORK_TYPE_HSPA:
t = 12;
break;
case ContextManager.MDM_NETWORK_TYPE_HSPAP:
t = 13;
break;
case ContextManager.MDM_NETWORK_TYPE_EHRPD:
t = 14;
break;
case ContextManager.MDM_NETWORK_TYPE_LTE:
t = 15;
break;
default:
t = 16;
}
return t;
}
I really wouldn't count on that behavior.

Categories

Resources