corrected code style

This commit is contained in:
Adithya Abraham Philip 2015-03-04 04:14:28 +05:30
parent c2c8ee1efd
commit 024ba19499

View file

@ -100,7 +100,7 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) { switch (item.getItemId()) {
case R.id.menu_log_display_export_log: case R.id.menu_log_display_export_log:
exportLog(); exportLog();
break; break;
@ -116,100 +116,94 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe
private void writeToLogFile(final OperationResult.OperationLog operationLog, final File f) { private void writeToLogFile(final OperationResult.OperationLog operationLog, final File f) {
OperationResult.OperationLog currLog = new OperationResult.OperationLog(); OperationResult.OperationLog currLog = new OperationResult.OperationLog();
currLog.add(OperationResult.LogType.MSG_EXPORT_LOG,0); currLog.add(OperationResult.LogType.MSG_EXPORT_LOG, 0);
boolean error = false; boolean error = false;
PrintWriter pw = null; PrintWriter pw = null;
try { try {
pw = new PrintWriter(f); pw = new PrintWriter(f);
pw.print(getPrintableOperationLog(operationLog,"")); pw.print(getPrintableOperationLog(operationLog, ""));
if(pw.checkError()) {//IOException if (pw.checkError()) {//IOException
Log.e(Constants.TAG, "Log Export I/O Exception "+f.getAbsolutePath()); Log.e(Constants.TAG, "Log Export I/O Exception " + f.getAbsolutePath());
currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_WRITING,1); currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_WRITING, 1);
error = true; error = true;
} }
} catch(FileNotFoundException e) { } catch (FileNotFoundException e) {
Log.e(Constants.TAG, "File not found for exporting log "+f.getAbsolutePath()); Log.e(Constants.TAG, "File not found for exporting log " + f.getAbsolutePath());
currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_FOPEN, 1); currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_FOPEN, 1);
error = true; error = true;
} }
if(pw!=null) { if (pw != null) {
pw.close(); pw.close();
if(!error && pw.checkError()) {//check if it is only pw.close() which generated error if (!error && pw.checkError()) {//check if it is only pw.close() which generated error
currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_WRITING,1); currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_WRITING, 1);
error = true; error = true;
} }
} }
if(!error) currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_SUCCESS,1); if (!error) currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_SUCCESS, 1);
int opResultCode = error?OperationResult.RESULT_ERROR:OperationResult.RESULT_OK; int opResultCode = error ? OperationResult.RESULT_ERROR : OperationResult.RESULT_OK;
OperationResult opResult = new LogExportResult(opResultCode,currLog); OperationResult opResult = new LogExportResult(opResultCode, currLog);
opResult.createNotify(getActivity()).show(); opResult.createNotify(getActivity()).show();
} }
private static class LogExportResult extends OperationResult {
public LogExportResult(int result, OperationLog log) {
super(result, log);
}
/** trivial but necessary to implement the Parcelable protocol. */
public LogExportResult(Parcel source) {
super(source);
}
public static Creator<LogExportResult> CREATOR = new Creator<LogExportResult>() {
public LogExportResult createFromParcel(final Parcel source) {
return new LogExportResult(source);
}
public LogExportResult[] newArray(final int size) {
return new LogExportResult[size];
}
};
}
/** /**
* returns an indented String of an entire OperationLog * returns an indented String of an entire OperationLog
* @param operationLog log to be converted to indented, printable format *
* @param opLog log to be converted to indented, printable format
* @param basePadding padding to add at the start of all log entries, made for use with SubLogs * @param basePadding padding to add at the start of all log entries, made for use with SubLogs
* @return printable, indented version of passed operationLog * @return printable, indented version of passed operationLog
*/ */
private String getPrintableOperationLog(OperationResult.OperationLog operationLog, String basePadding) { private String getPrintableOperationLog(OperationResult.OperationLog opLog, String basePadding) {
String log = ""; String log = "";
Iterator<LogEntryParcel> logIterator = operationLog.iterator(); for (Iterator<LogEntryParcel> logIterator = opLog.iterator(); logIterator.hasNext(); ) {
while(logIterator.hasNext()) { log += getPrintableLogEntry(logIterator.next(), basePadding) + "\n";
log += getPrintableLogEntry(logIterator.next(), basePadding)+"\n";
} }
log = log.substring(0,log.length()-1);//gets rid of extra new line log = log.substring(0, log.length() - 1);//gets rid of extra new line
return log; return log;
} }
/** /**
* returns an indented String of a LogEntryParcel including any sub-logs it may contain * returns an indented String of a LogEntryParcel including any sub-logs it may contain
*
* @param entryParcel log entryParcel whose String representation is to be obtained * @param entryParcel log entryParcel whose String representation is to be obtained
* @return indented version of passed log entryParcel in a readable format * @return indented version of passed log entryParcel in a readable format
*/ */
private String getPrintableLogEntry(OperationResult.LogEntryParcel entryParcel, private String getPrintableLogEntry(OperationResult.LogEntryParcel entryParcel,
String basePadding) { String basePadding) {
String logText = "";
final String indent = " ";//4 spaces = 1 Indent level final String indent = " ";//4 spaces = 1 Indent level
String padding = basePadding; String padding = basePadding;
for(int i =0;i<entryParcel.mIndent;i++) { for (int i = 0; i < entryParcel.mIndent; i++) {
padding +=indent; padding += indent;
} }
logText = padding; String logText = padding;
switch (entryParcel.mType.mLevel) { switch (entryParcel.mType.mLevel) {
case DEBUG: logText+="[DEBUG]"; break; case DEBUG:
case INFO: logText+="[INFO]"; break; logText += "[DEBUG]";
case WARN: logText+="[WARN]"; break; break;
case ERROR: logText+="[ERROR]"; break; case INFO:
case START: logText+="[START]"; break; logText += "[INFO]";
case OK: logText+="[OK]"; break; break;
case CANCELLED: logText+="[CANCELLED]"; break; case WARN:
logText += "[WARN]";
break;
case ERROR:
logText += "[ERROR]";
break;
case START:
logText += "[START]";
break;
case OK:
logText += "[OK]";
break;
case CANCELLED:
logText += "[CANCELLED]";
break;
} }
// special case: first parameter may be a quantity // special case: first parameter may be a quantity
@ -224,12 +218,11 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe
} }
if (entryParcel instanceof SubLogEntryParcel) { if (entryParcel instanceof SubLogEntryParcel) {
OperationResult subResult = ((SubLogEntryParcel) entryParcel).getSubResult(); OperationResult subResult = ((SubLogEntryParcel) entryParcel).getSubResult();
LogEntryParcel subEntry = subResult.getLog().getLast(); LogEntryParcel subEntry = subResult.getLog().getLast();
if (subEntry != null) { if (subEntry != null) {
//the first line of log of subResult is same as entryParcel, so replace logText //the first line of log of subResult is same as entryParcel, so replace logText
logText = getPrintableOperationLog(subResult.getLog(),padding); logText = getPrintableOperationLog(subResult.getLog(), padding);
} }
} }
@ -245,11 +238,35 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe
FileHelper.saveFile(new FileHelper.FileDialogCallback() { FileHelper.saveFile(new FileHelper.FileDialogCallback() {
@Override @Override
public void onFileSelected(File file, boolean checked) { public void onFileSelected(File file, boolean checked) {
writeToLogFile(mResult.getLog(),file); writeToLogFile(mResult.getLog(), file);
} }
}, this.getActivity().getSupportFragmentManager(), title, message, exportFile, null); }, this.getActivity().getSupportFragmentManager(), title, message, exportFile, null);
} }
private static class LogExportResult extends OperationResult {
public static Creator<LogExportResult> CREATOR = new Creator<LogExportResult>() {
public LogExportResult createFromParcel(final Parcel source) {
return new LogExportResult(source);
}
public LogExportResult[] newArray(final int size) {
return new LogExportResult[size];
}
};
public LogExportResult(int result, OperationLog log) {
super(result, log);
}
/**
* trivial but necessary to implement the Parcelable protocol.
*/
public LogExportResult(Parcel source) {
super(source);
}
}
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
LogEntryParcel parcel = mAdapter.getItem(position); LogEntryParcel parcel = mAdapter.getItem(position);