Android: Move translateRPCStatus to BOINCUtils.java. Remove duplicate code from list adapters.

This commit is contained in:
Rom Walton 2013-02-26 19:11:38 -05:00 committed by Oliver Bock
parent 58dadaf9cb
commit 3188b38195
8 changed files with 107 additions and 120 deletions

View File

@ -20,7 +20,7 @@ package edu.berkeley.boinc;
import edu.berkeley.boinc.client.ClientStatus;
import edu.berkeley.boinc.client.Monitor;
import edu.berkeley.boinc.definitions.CommonDefs;
import edu.berkeley.boinc.utils.BOINCDefs;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
@ -41,7 +41,6 @@ public class StatusActivity extends Activity {
private final String TAG = "BOINC StatusActivity";
private Monitor monitor;
private Boolean mIsBound = false;
private BroadcastReceiver mClientStatusChangeRec = new BroadcastReceiver() {
@ -150,52 +149,52 @@ public class StatusActivity extends Activity {
changeRunmodeDescriptor.setText(R.string.disable_computation);
changeRunmodeDescriptor.setTag(false);
switch(status.computingSuspendReason) {
case CommonDefs.SUSPEND_REASON_BATTERIES:
case BOINCDefs.SUSPEND_REASON_BATTERIES:
statusDescriptor.setText(R.string.suspend_batteries);
break;
case CommonDefs.SUSPEND_REASON_USER_ACTIVE:
case BOINCDefs.SUSPEND_REASON_USER_ACTIVE:
statusDescriptor.setText(R.string.suspend_useractive);
break;
case CommonDefs.SUSPEND_REASON_USER_REQ:
case BOINCDefs.SUSPEND_REASON_USER_REQ:
statusDescriptor.setText(R.string.suspend_userreq);
break;
case CommonDefs.SUSPEND_REASON_TIME_OF_DAY:
case BOINCDefs.SUSPEND_REASON_TIME_OF_DAY:
statusDescriptor.setText(R.string.suspend_tod);
break;
case CommonDefs.SUSPEND_REASON_BENCHMARKS:
case BOINCDefs.SUSPEND_REASON_BENCHMARKS:
statusDescriptor.setText(R.string.suspend_bm);
break;
case CommonDefs.SUSPEND_REASON_DISK_SIZE:
case BOINCDefs.SUSPEND_REASON_DISK_SIZE:
statusDescriptor.setText(R.string.suspend_disksize);
break;
case CommonDefs.SUSPEND_REASON_CPU_THROTTLE:
case BOINCDefs.SUSPEND_REASON_CPU_THROTTLE:
statusDescriptor.setText(R.string.suspend_cputhrottle);
break;
case CommonDefs.SUSPEND_REASON_NO_RECENT_INPUT:
case BOINCDefs.SUSPEND_REASON_NO_RECENT_INPUT:
statusDescriptor.setText(R.string.suspend_noinput);
break;
case CommonDefs.SUSPEND_REASON_INITIAL_DELAY:
case BOINCDefs.SUSPEND_REASON_INITIAL_DELAY:
statusDescriptor.setText(R.string.suspend_delay);
break;
case CommonDefs.SUSPEND_REASON_EXCLUSIVE_APP_RUNNING:
case BOINCDefs.SUSPEND_REASON_EXCLUSIVE_APP_RUNNING:
statusDescriptor.setText(R.string.suspend_exclusiveapp);
break;
case CommonDefs.SUSPEND_REASON_CPU_USAGE:
case BOINCDefs.SUSPEND_REASON_CPU_USAGE:
statusDescriptor.setText(R.string.suspend_cpu);
break;
case CommonDefs.SUSPEND_REASON_NETWORK_QUOTA_EXCEEDED:
case BOINCDefs.SUSPEND_REASON_NETWORK_QUOTA_EXCEEDED:
statusDescriptor.setText(R.string.suspend_network_quota);
break;
case CommonDefs.SUSPEND_REASON_OS:
case BOINCDefs.SUSPEND_REASON_OS:
statusDescriptor.setText(R.string.suspend_os);
break;
case CommonDefs.SUSPEND_REASON_WIFI_STATE:
case BOINCDefs.SUSPEND_REASON_WIFI_STATE:
statusDescriptor.setText(R.string.suspend_wifi);
break;
case CommonDefs.SUSPEND_REASON_BATTERY_CHARGING:
case BOINCDefs.SUSPEND_REASON_BATTERY_CHARGING:
statusDescriptor.setText(R.string.suspend_battery_charging);
break;
case CommonDefs.SUSPEND_REASON_BATTERY_OVERHEATED:
case BOINCDefs.SUSPEND_REASON_BATTERY_OVERHEATED:
statusDescriptor.setText(R.string.suspend_battery_overheating);
break;
default:
@ -216,7 +215,8 @@ public class StatusActivity extends Activity {
try{
networkState = status.networkSuspendReason;
} catch (Exception e) {}
if(networkState==8192){ //network suspended due to wifi state
if(networkState == BOINCDefs.SUSPEND_REASON_WIFI_STATE){
// Network suspended due to wifi state
statusDescriptor.setText(R.string.suspend_wifi);
}else {
statusDescriptor.setText(R.string.status_idle_long);
@ -246,9 +246,9 @@ public class StatusActivity extends Activity {
try {
Boolean enable = (Boolean) v.getTag();
if(enable) {
monitor.setRunMode(CommonDefs.RUN_MODE_AUTO);
monitor.setRunMode(BOINCDefs.RUN_MODE_AUTO);
} else {
monitor.setRunMode(CommonDefs.RUN_MODE_NEVER);
monitor.setRunMode(BOINCDefs.RUN_MODE_NEVER);
}
} catch (Exception e) {Log.e(TAG, "could not map status tag", e);}
}

View File

@ -34,10 +34,10 @@ import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import edu.berkeley.boinc.definitions.CommonDefs;
import edu.berkeley.boinc.ProjectsActivity;
import edu.berkeley.boinc.R;
import edu.berkeley.boinc.rpc.Project;
import edu.berkeley.boinc.utils.BOINCUtils;
public class ProjectsListAdapter extends ArrayAdapter<Project> implements OnItemClickListener {
@ -106,7 +106,7 @@ public class ProjectsListAdapter extends ArrayAdapter<Project> implements OnItem
if (project.sched_rpc_pending > 0) {
appendToStatus(sb, activity.getResources().getString(R.string.projects_status_schedrpcpending));
appendToStatus(sb,
translateRPCReason(project.sched_rpc_pending)
BOINCUtils.translateRPCReason(activity, project.sched_rpc_pending)
);
}
if (project.scheduler_rpc_in_progress) {
@ -130,27 +130,6 @@ public class ProjectsListAdapter extends ArrayAdapter<Project> implements OnItem
return sb.toString();
}
private String translateRPCReason(int reason) {
switch (reason) {
case CommonDefs.RPC_REASON_USER_REQ:
return activity.getResources().getString(R.string.rpcreason_userreq);
case CommonDefs.RPC_REASON_NEED_WORK:
return activity.getResources().getString(R.string.rpcreason_needwork);
case CommonDefs.RPC_REASON_RESULTS_DUE:
return activity.getResources().getString(R.string.rpcreason_resultsdue);
case CommonDefs.RPC_REASON_TRICKLE_UP:
return activity.getResources().getString(R.string.rpcreason_trickleup);
case CommonDefs.RPC_REASON_ACCT_MGR_REQ:
return activity.getResources().getString(R.string.rpcreason_acctmgrreq);
case CommonDefs.RPC_REASON_INIT:
return activity.getResources().getString(R.string.rpcreason_init);
case CommonDefs.RPC_REASON_PROJECT_REQ:
return activity.getResources().getString(R.string.rpcreason_projectreq);
default:
return activity.getResources().getString(R.string.rpcreason_unknown);
}
}
private void appendToStatus(StringBuffer existing, String additional) {
if (existing.length() == 0) {
existing.append(additional);

View File

@ -21,8 +21,8 @@ package edu.berkeley.boinc.adapter;
import java.util.ArrayList;
import edu.berkeley.boinc.R;
import edu.berkeley.boinc.definitions.CommonDefs;
import edu.berkeley.boinc.rpc.Result;
import edu.berkeley.boinc.utils.BOINCDefs;
import android.app.Activity;
import android.content.Context;
@ -132,7 +132,7 @@ public class TasksListAdapter extends ArrayAdapter<Result>{
private Integer determineProgressBarLayout(Result tmp) {
if(tmp.active_task) {
if(tmp.active_task_state == CommonDefs.PROCESS_EXECUTING) {
if(tmp.active_task_state == BOINCDefs.PROCESS_EXECUTING) {
//running
return R.drawable.progressbar_active;
} else {
@ -140,7 +140,7 @@ public class TasksListAdapter extends ArrayAdapter<Result>{
return R.drawable.progressbar_paused;
}
} else {
if((tmp.state == CommonDefs.RESULT_ABORTED) || (tmp.state == CommonDefs.RESULT_UPLOAD_FAILED) || (tmp.state == CommonDefs.RESULT_COMPUTE_ERROR)) {
if((tmp.state == BOINCDefs.RESULT_ABORTED) || (tmp.state == BOINCDefs.RESULT_UPLOAD_FAILED) || (tmp.state == BOINCDefs.RESULT_COMPUTE_ERROR)) {
//error
return R.drawable.progressbar_error;
} else if (tmp.ready_to_report) {

View File

@ -21,7 +21,6 @@ package edu.berkeley.boinc.adapter;
import java.util.ArrayList;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.text.format.DateUtils;
@ -38,10 +37,10 @@ import android.widget.ProgressBar;
import android.widget.TextView;
import edu.berkeley.boinc.TransActivity;
import edu.berkeley.boinc.R;
import edu.berkeley.boinc.definitions.BOINCErrors;
import edu.berkeley.boinc.definitions.CommonDefs;
import edu.berkeley.boinc.rpc.Transfer;
import edu.berkeley.boinc.rpc.CcStatus;
import edu.berkeley.boinc.utils.BOINCErrors;
import edu.berkeley.boinc.utils.BOINCUtils;
public class TransListAdapter extends ArrayAdapter<Transfer> implements OnItemClickListener {
@ -140,7 +139,7 @@ public class TransListAdapter extends ArrayAdapter<Transfer> implements OnItemCl
if (status.network_suspend_reason > 0) {
buf += activity.getResources().getString(R.string.trans_suspended);
buf += " - ";
buf += translateRPCReason(status.network_suspend_reason);
buf += BOINCUtils.translateRPCReason(activity, status.network_suspend_reason);
} else {
if (transfer.xfer_active) {
buf += activity.getResources().getString(R.string.trans_active);
@ -160,64 +159,6 @@ public class TransListAdapter extends ArrayAdapter<Transfer> implements OnItemCl
return buf;
}
private String translateRPCReason(int reason) {
switch (reason) {
case CommonDefs.RPC_REASON_USER_REQ:
return activity.getResources().getString(R.string.rpcreason_userreq);
case CommonDefs.RPC_REASON_NEED_WORK:
return activity.getResources().getString(R.string.rpcreason_needwork);
case CommonDefs.RPC_REASON_RESULTS_DUE:
return activity.getResources().getString(R.string.rpcreason_resultsdue);
case CommonDefs.RPC_REASON_TRICKLE_UP:
return activity.getResources().getString(R.string.rpcreason_trickleup);
case CommonDefs.RPC_REASON_ACCT_MGR_REQ:
return activity.getResources().getString(R.string.rpcreason_acctmgrreq);
case CommonDefs.RPC_REASON_INIT:
return activity.getResources().getString(R.string.rpcreason_init);
case CommonDefs.RPC_REASON_PROJECT_REQ:
return activity.getResources().getString(R.string.rpcreason_projectreq);
default:
return activity.getResources().getString(R.string.rpcreason_unknown);
}
}
@SuppressLint("DefaultLocale")
private String formatSize(double fBytesSent, double fFileSize) {
String buf = new String();
double xTera = 1099511627776.0;
double xGiga = 1073741824.0;
double xMega = 1048576.0;
double xKilo = 1024.0;
if (fFileSize != 0) {
if (fFileSize >= xTera) {
buf = String.format("%0.2f/%0.2f TB", fBytesSent/xTera, fFileSize/xTera);
} else if (fFileSize >= xGiga) {
buf = String.format("%0.2f/%0.2f GB", fBytesSent/xGiga, fFileSize/xGiga);
} else if (fFileSize >= xMega) {
buf = String.format("%0.2f/%0.2f MB", fBytesSent/xMega, fFileSize/xMega);
} else if (fFileSize >= xKilo) {
buf = String.format("%0.2f/%0.2f KB", fBytesSent/xKilo, fFileSize/xKilo);
} else {
buf = String.format("%0.0f/%0.0f bytes", fBytesSent, fFileSize);
}
} else {
if (fBytesSent >= xTera) {
buf = String.format("%0.2f TB", fBytesSent/xTera);
} else if (fBytesSent >= xGiga) {
buf = String.format("%0.2f GB", fBytesSent/xGiga);
} else if (fBytesSent >= xMega) {
buf = String.format("%0.2f MB", fBytesSent/xMega);
} else if (fBytesSent >= xKilo) {
buf = String.format("%0.2f KB", fBytesSent/xKilo);
} else {
buf = String.format("%0.0f bytes", fBytesSent);
}
}
return buf;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
@ -247,7 +188,7 @@ public class TransListAdapter extends ArrayAdapter<Transfer> implements OnItemCl
// Populate UI Elements
viewTransfer.entryIndex = position;
viewTransfer.tvName.setText(getName(position));
viewTransfer.tvProgress.setText(formatSize(getItem(position).bytes_xferred, getItem(position).nbytes));
viewTransfer.tvProgress.setText(BOINCUtils.formatSize(getItem(position).bytes_xferred, getItem(position).nbytes));
viewTransfer.pbProgressBar.setIndeterminate(false);
viewTransfer.pbProgressBar.setProgress(getProgress(position));

View File

@ -24,13 +24,13 @@ import android.content.Context;
import android.content.Intent;
import android.util.Log;
import edu.berkeley.boinc.BOINCActivity;
import edu.berkeley.boinc.definitions.CommonDefs;
import edu.berkeley.boinc.rpc.CcStatus;
import edu.berkeley.boinc.rpc.GlobalPreferences;
import edu.berkeley.boinc.rpc.Message;
import edu.berkeley.boinc.rpc.Project;
import edu.berkeley.boinc.rpc.Result;
import edu.berkeley.boinc.rpc.Transfer;
import edu.berkeley.boinc.utils.BOINCDefs;
/*
* Singleton that holds the client status data, as determined by the Monitor.
@ -198,19 +198,19 @@ public class ClientStatus {
private void parseComputingStatus() {
computingParseError = true;
try {
if(status.task_mode==CommonDefs.RUN_MODE_NEVER) {
if(status.task_mode==BOINCDefs.RUN_MODE_NEVER) {
computingStatus = COMPUTING_STATUS_NEVER;
computingSuspendReason = status.task_suspend_reason; // = 4 - SUSPEND_REASON_USER_REQ????
computingParseError = false;
return;
}
if((status.task_mode == CommonDefs.RUN_MODE_AUTO) && (status.task_suspend_reason != CommonDefs.SUSPEND_NOT_SUSPENDED)) {
if((status.task_mode == BOINCDefs.RUN_MODE_AUTO) && (status.task_suspend_reason != BOINCDefs.SUSPEND_NOT_SUSPENDED)) {
computingStatus = COMPUTING_STATUS_SUSPENDED;
computingSuspendReason = status.task_suspend_reason;
computingParseError = false;
return;
}
if((status.task_mode == CommonDefs.RUN_MODE_AUTO) && (status.task_suspend_reason == CommonDefs.SUSPEND_NOT_SUSPENDED)) {
if((status.task_mode == BOINCDefs.RUN_MODE_AUTO) && (status.task_suspend_reason == BOINCDefs.SUSPEND_NOT_SUSPENDED)) {
//figure out whether we have an active task
Boolean activeTask = false;
if(results!=null) {
@ -243,19 +243,19 @@ public class ClientStatus {
private void parseNetworkStatus() {
networkParseError = true;
try {
if(status.network_mode==CommonDefs.RUN_MODE_NEVER) {
if(status.network_mode==BOINCDefs.RUN_MODE_NEVER) {
networkStatus = NETWORK_STATUS_NEVER;
networkSuspendReason = status.network_suspend_reason; // = 4 - SUSPEND_REASON_USER_REQ????
networkParseError = false;
return;
}
if((status.network_mode == CommonDefs.RUN_MODE_AUTO) && (status.network_suspend_reason != CommonDefs.SUSPEND_NOT_SUSPENDED)) {
if((status.network_mode == BOINCDefs.RUN_MODE_AUTO) && (status.network_suspend_reason != BOINCDefs.SUSPEND_NOT_SUSPENDED)) {
networkStatus = NETWORK_STATUS_SUSPENDED;
networkSuspendReason = status.network_suspend_reason;
networkParseError = false;
return;
}
if((status.network_mode == CommonDefs.RUN_MODE_AUTO) && (status.network_suspend_reason == CommonDefs.SUSPEND_NOT_SUSPENDED)) {
if((status.network_mode == BOINCDefs.RUN_MODE_AUTO) && (status.network_suspend_reason == BOINCDefs.SUSPEND_NOT_SUSPENDED)) {
networkStatus = NETWORK_STATUS_AVAILABLE;
networkSuspendReason = status.network_suspend_reason; // = 0 - SUSPEND_NOT_SUSPENDED
networkParseError = false;

View File

@ -16,13 +16,13 @@
* You should have received a copy of the GNU Lesser General Public License
* along with BOINC. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package edu.berkeley.boinc.definitions;
package edu.berkeley.boinc.utils;
/*
* This tries to be the same as lib/common_defs.h
*/
public class CommonDefs {
public class BOINCDefs {
public static final int GUI_RPC_PORT = 31416;

View File

@ -16,7 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with BOINC. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package edu.berkeley.boinc.definitions;
package edu.berkeley.boinc.utils;
/*
* This tries to be the same as lib/error_numbers.h

View File

@ -0,0 +1,67 @@
package edu.berkeley.boinc.utils;
import android.annotation.SuppressLint;
import android.app.Activity;
import edu.berkeley.boinc.R;
public class BOINCUtils {
public static String translateRPCReason(Activity activity, int reason) {
switch (reason) {
case BOINCDefs.RPC_REASON_USER_REQ:
return activity.getResources().getString(R.string.rpcreason_userreq);
case BOINCDefs.RPC_REASON_NEED_WORK:
return activity.getResources().getString(R.string.rpcreason_needwork);
case BOINCDefs.RPC_REASON_RESULTS_DUE:
return activity.getResources().getString(R.string.rpcreason_resultsdue);
case BOINCDefs.RPC_REASON_TRICKLE_UP:
return activity.getResources().getString(R.string.rpcreason_trickleup);
case BOINCDefs.RPC_REASON_ACCT_MGR_REQ:
return activity.getResources().getString(R.string.rpcreason_acctmgrreq);
case BOINCDefs.RPC_REASON_INIT:
return activity.getResources().getString(R.string.rpcreason_init);
case BOINCDefs.RPC_REASON_PROJECT_REQ:
return activity.getResources().getString(R.string.rpcreason_projectreq);
default:
return activity.getResources().getString(R.string.rpcreason_unknown);
}
}
@SuppressLint("DefaultLocale")
public static String formatSize(double fBytesSent, double fFileSize) {
String buf = new String();
double xTera = 1099511627776.0;
double xGiga = 1073741824.0;
double xMega = 1048576.0;
double xKilo = 1024.0;
if (fFileSize != 0) {
if (fFileSize >= xTera) {
buf = String.format("%0.2f/%0.2f TB", fBytesSent/xTera, fFileSize/xTera);
} else if (fFileSize >= xGiga) {
buf = String.format("%0.2f/%0.2f GB", fBytesSent/xGiga, fFileSize/xGiga);
} else if (fFileSize >= xMega) {
buf = String.format("%0.2f/%0.2f MB", fBytesSent/xMega, fFileSize/xMega);
} else if (fFileSize >= xKilo) {
buf = String.format("%0.2f/%0.2f KB", fBytesSent/xKilo, fFileSize/xKilo);
} else {
buf = String.format("%0.0f/%0.0f bytes", fBytesSent, fFileSize);
}
} else {
if (fBytesSent >= xTera) {
buf = String.format("%0.2f TB", fBytesSent/xTera);
} else if (fBytesSent >= xGiga) {
buf = String.format("%0.2f GB", fBytesSent/xGiga);
} else if (fBytesSent >= xMega) {
buf = String.format("%0.2f MB", fBytesSent/xMega);
} else if (fBytesSent >= xKilo) {
buf = String.format("%0.2f KB", fBytesSent/xKilo);
} else {
buf = String.format("%0.0f bytes", fBytesSent);
}
}
return buf;
}
}