From ec353b0c390f7b3f45b950171e56c6ddcd0a9607 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Thu, 26 Feb 2015 21:58:25 +0530 Subject: initial Log Display function --- .../keychain/ui/LogDisplayFragment.java | 102 +++++++++++++++++++++ .../res/drawable-hdpi/ic_action_save_white.png | Bin 0 -> 394 bytes .../res/drawable-mdpi/ic_action_save_white.png | Bin 0 -> 362 bytes .../res/drawable-xhdpi/ic_action_save_white.png | Bin 0 -> 441 bytes .../res/drawable-xxhdpi/ic_action_save_white.png | Bin 0 -> 495 bytes OpenKeychain/src/main/res/menu/log_display.xml | 11 +++ OpenKeychain/src/main/res/values/strings.xml | 4 + 7 files changed, 117 insertions(+) create mode 100644 OpenKeychain/src/main/res/drawable-hdpi/ic_action_save_white.png create mode 100644 OpenKeychain/src/main/res/drawable-mdpi/ic_action_save_white.png create mode 100644 OpenKeychain/src/main/res/drawable-xhdpi/ic_action_save_white.png create mode 100644 OpenKeychain/src/main/res/drawable-xxhdpi/ic_action_save_white.png create mode 100644 OpenKeychain/src/main/res/menu/log_display.xml (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java index 2baebc83d..a44274c67 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java @@ -23,8 +23,13 @@ import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.ListFragment; +import android.support.v4.view.MenuItemCompat; +import android.support.v7.widget.SearchView; import android.util.TypedValue; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; @@ -32,12 +37,26 @@ import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; +import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.operations.results.OperationResult; import org.sufficientlysecure.keychain.operations.results.OperationResult.LogEntryParcel; import org.sufficientlysecure.keychain.operations.results.OperationResult.LogLevel; import org.sufficientlysecure.keychain.operations.results.OperationResult.SubLogEntryParcel; +import org.sufficientlysecure.keychain.provider.KeychainContract; +import org.sufficientlysecure.keychain.provider.KeychainDatabase; +import org.sufficientlysecure.keychain.ui.util.Notify; +import org.sufficientlysecure.keychain.util.ExportHelper; +import org.sufficientlysecure.keychain.util.FileHelper; +import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Preferences; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Iterator; public class LogDisplayFragment extends ListFragment implements OnItemClickListener { @@ -46,6 +65,12 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe OperationResult mResult; public static final String EXTRA_RESULT = "log"; + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setHasOptionsMenu(true); + } @Override public void onActivityCreated(Bundle savedInstanceState) { @@ -70,6 +95,83 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe getListView().setFastScrollEnabled(true); getListView().setDividerHeight(0); + + } + + @Override + public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { + Toast.makeText(this.getActivity(),"Options created",Toast.LENGTH_SHORT).show(); + inflater.inflate(R.menu.log_display, menu); + + super.onCreateOptionsMenu(menu, inflater); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch(item.getItemId()) { + case R.id.menu_log_display_export_log: + exportLog(); + break; + } + + return super.onOptionsItemSelected(item); + } + + private void exportLog() { + Toast.makeText(this.getActivity(),"Exporting log",Toast.LENGTH_LONG).show(); + showExportLogDialog(new File(Constants.Path.APP_DIR, "export.log")); + } + + private void writeToLogFile(final OperationResult.OperationLog operationLog, final File f) { + + PrintWriter pw = null; + try { + pw = new PrintWriter(f); + } catch(IOException e) { + e.printStackTrace(); + } + + Iterator logIterator = operationLog.iterator(); + + while(logIterator.hasNext()) { + + pw.println(getPrintableLogEntry(logIterator.next())); + } + + pw.close(); + } + + /** + * returns an indented String of a LogEntryParcel + * @param entry log entry whose String representation is to be obtained + * @return the passed log entry in a readable format + */ + private String getPrintableLogEntry(OperationResult.LogEntryParcel entry) { + String printable = ""; + + return entry.toString(); + } + + private void showExportLogDialog(final File exportFile) { + + String title = this.getString(R.string.title_export_log); + + String message = this.getString(R.string.specify_file_to_export_log_to); + + FileHelper.saveFile(new FileHelper.FileDialogCallback() { + @Override + public void onFileSelected(File file, boolean checked) { + writeToLogFile(getOperationLog(),file); + } + }, this.getActivity().getSupportFragmentManager(), title, message, exportFile, null); + } + + private OperationResult.OperationLog getOperationLog(){ + OperationResult operationResult = this.getActivity().getIntent().getParcelableExtra( + LogDisplayFragment.EXTRA_RESULT); + OperationResult.OperationLog operationResultLog = operationResult.getLog(); + + return operationResultLog; } @Override diff --git a/OpenKeychain/src/main/res/drawable-hdpi/ic_action_save_white.png b/OpenKeychain/src/main/res/drawable-hdpi/ic_action_save_white.png new file mode 100644 index 000000000..0fe36a1ec Binary files /dev/null and b/OpenKeychain/src/main/res/drawable-hdpi/ic_action_save_white.png differ diff --git a/OpenKeychain/src/main/res/drawable-mdpi/ic_action_save_white.png b/OpenKeychain/src/main/res/drawable-mdpi/ic_action_save_white.png new file mode 100644 index 000000000..664260d8c Binary files /dev/null and b/OpenKeychain/src/main/res/drawable-mdpi/ic_action_save_white.png differ diff --git a/OpenKeychain/src/main/res/drawable-xhdpi/ic_action_save_white.png b/OpenKeychain/src/main/res/drawable-xhdpi/ic_action_save_white.png new file mode 100644 index 000000000..dde278b5e Binary files /dev/null and b/OpenKeychain/src/main/res/drawable-xhdpi/ic_action_save_white.png differ diff --git a/OpenKeychain/src/main/res/drawable-xxhdpi/ic_action_save_white.png b/OpenKeychain/src/main/res/drawable-xxhdpi/ic_action_save_white.png new file mode 100644 index 000000000..ccf8c82cd Binary files /dev/null and b/OpenKeychain/src/main/res/drawable-xxhdpi/ic_action_save_white.png differ diff --git a/OpenKeychain/src/main/res/menu/log_display.xml b/OpenKeychain/src/main/res/menu/log_display.xml new file mode 100644 index 000000000..03af3c5ad --- /dev/null +++ b/OpenKeychain/src/main/res/menu/log_display.xml @@ -0,0 +1,11 @@ + + + + + + diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index e01f1c049..9d980500d 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -42,6 +42,7 @@ "Exchange Keys" "Advanced Key Info" "Keys" + "Export Log" "Identities" @@ -106,6 +107,7 @@ "Search cloud" "Export all keys" "Show advanced info" + "Export Log" "Message" @@ -237,6 +239,8 @@ "Note: only subkeys support ElGamal." "Couldn't find key %08X." + "Please specify file to export to. \nWARNING: File will be overwritten if it exists." + " "%d bad secret key ignored. Perhaps you exported with the option\n --export-secret-subkeys\nMake sure you export with\n --export-secret-keys\ninstead."" "%d bad secret keys ignored. Perhaps you exported with the option\n --export-secret-subkeys\nMake sure you export with\n --export-secret-keys\ninstead."" -- cgit v1.2.3 From d776bd2888907b3bc953f48e9c490afe8275c4e1 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Sat, 28 Feb 2015 01:30:17 +0530 Subject: refined log output --- .../keychain/ui/LogDisplayFragment.java | 75 ++++++++++++++++++++-- 1 file changed, 71 insertions(+), 4 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java index ec7eee3ac..94d9a60c9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java @@ -147,9 +147,75 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe * @return the passed log entry in a readable format */ private String getPrintableLogEntry(OperationResult.LogEntryParcel entry) { - String printable = ""; + String subLogText=null; + if (entry instanceof SubLogEntryParcel) { + Log.d("ADI DisplayFragment",entry.toString()); + + OperationResult result = ((SubLogEntryParcel) entry).getSubResult(); + LogEntryParcel subEntry = result.getLog().getLast(); + if (subEntry != null) { + String subPadding=""; + for(int i=0;i 0 + && subEntry.mParameters[0] instanceof Integer) { + subLogText+=getResources().getQuantityString(subEntry.mType.getMsgId(), + (Integer) subEntry.mParameters[0], + subEntry.mParameters); + } else { + subLogText+=getResources().getString(subEntry.mType.getMsgId(), + subEntry.mParameters); + } + } + + } else { + Log.d("ADIExportLogDisplayFragment","NOTSUBLOG:"+entry.toString()); + } + + String logText = ""; + switch (entry.mType.mLevel) { + case DEBUG: logText="[DEBUG]"; break; + case INFO: logText="[INFO]"; break; + case WARN: logText="[WARN]"; break; + case ERROR: logText="[WARN]"; break; + case START: logText="[START]"; break; + case OK: logText="[OK]"; break; + case CANCELLED: logText="[CANCELLED]"; break; + } - return entry.toString(); + // special case: first parameter may be a quantity + if (entry.mParameters != null && entry.mParameters.length > 0 + && entry.mParameters[0] instanceof Integer) { + logText += getResources().getQuantityString(entry.mType.getMsgId(), + (Integer) entry.mParameters[0], + entry.mParameters); + } else { + logText += getResources().getString(entry.mType.getMsgId(), + entry.mParameters); + } + + String padding = ""; + for(int i =0;i Date: Sat, 28 Feb 2015 02:11:39 +0530 Subject: cleaned up log export --- .../keychain/ui/LogDisplayFragment.java | 29 +++++++--------------- 1 file changed, 9 insertions(+), 20 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java index 94d9a60c9..997b977ff 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java @@ -118,7 +118,7 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe } private void exportLog() { - Toast.makeText(this.getActivity(),"Exporting log",Toast.LENGTH_LONG).show(); + showExportLogDialog(new File(Constants.Path.APP_DIR, "export.log")); } @@ -149,7 +149,6 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe private String getPrintableLogEntry(OperationResult.LogEntryParcel entry) { String subLogText=null; if (entry instanceof SubLogEntryParcel) { - Log.d("ADI DisplayFragment",entry.toString()); OperationResult result = ((SubLogEntryParcel) entry).getSubResult(); LogEntryParcel subEntry = result.getLog().getLast(); @@ -158,7 +157,7 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe for(int i=0;i parent, View view, int position, long id) { LogEntryParcel parcel = mAdapter.getItem(position); @@ -300,7 +291,6 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe if (entry instanceof SubLogEntryParcel) { ih.mSub.setVisibility(View.VISIBLE); convertView.setClickable(false); - Log.d("ADI DisplayFragment",entry.toString()); convertView.setPadding((entry.mIndent) * dipFactor, 0, 0, 0); OperationResult result = ((SubLogEntryParcel) entry).getSubResult(); @@ -332,7 +322,6 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe } } else { - Log.d("ADI DisplayFragment","NOT:"+entry.toString()); ih.mSub.setVisibility(View.GONE); ih.mSecond.setVisibility(View.GONE); convertView.setClickable(true); -- cgit v1.2.3 From 95e9e2ac30407722ac08fff055ac9839babbffc4 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Sun, 1 Mar 2015 02:02:39 +0530 Subject: added logging, notify for log export --- .../operations/results/OperationResult.java | 7 +++ .../keychain/ui/LogDisplayFragment.java | 72 ++++++++++++++++------ OpenKeychain/src/main/res/values/strings.xml | 7 +++ 3 files changed, 66 insertions(+), 20 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java index f79900aab..aef61075f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java @@ -707,6 +707,13 @@ public abstract class OperationResult implements Parcelable { MSG_DEL_CONSOLIDATE (LogLevel.DEBUG, R.string.msg_del_consolidate), MSG_DEL_OK (LogLevel.OK, R.plurals.msg_del_ok), MSG_DEL_FAIL (LogLevel.WARN, R.plurals.msg_del_fail), + + //export log + MSG_EXPORT_LOG(LogLevel.START,R.string.msg_export_log_start), + MSG_EXPORT_LOG_EXPORT_ERROR_NO_FILE(LogLevel.ERROR,R.string.msg_export_log_error_no_file), + MSG_EXPORT_LOG_EXPORT_ERROR_FOPEN(LogLevel.ERROR,R.string.msg_export_log_error_fopen), + MSG_EXPORT_LOG_EXPORT_ERROR_WRITING(LogLevel.ERROR,R.string.msg_export_log_error_writing), + MSG_EXPORT_LOG_EXPORT_SUCCESS (LogLevel.OK, R.string.msg_export_log_success), ; public final int mMsgId; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java index 997b977ff..bd77294c1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java @@ -22,9 +22,8 @@ import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; +import android.os.Parcel; import android.support.v4.app.ListFragment; -import android.support.v4.view.MenuItemCompat; -import android.support.v7.widget.SearchView; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.Menu; @@ -37,7 +36,6 @@ import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; -import android.widget.Toast; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; @@ -45,16 +43,11 @@ import org.sufficientlysecure.keychain.operations.results.OperationResult; import org.sufficientlysecure.keychain.operations.results.OperationResult.LogEntryParcel; import org.sufficientlysecure.keychain.operations.results.OperationResult.LogLevel; import org.sufficientlysecure.keychain.operations.results.OperationResult.SubLogEntryParcel; -import org.sufficientlysecure.keychain.provider.KeychainContract; -import org.sufficientlysecure.keychain.provider.KeychainDatabase; -import org.sufficientlysecure.keychain.ui.util.Notify; -import org.sufficientlysecure.keychain.util.ExportHelper; import org.sufficientlysecure.keychain.util.FileHelper; import org.sufficientlysecure.keychain.util.Log; -import org.sufficientlysecure.keychain.util.Preferences; import java.io.File; -import java.io.IOException; +import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Iterator; @@ -100,7 +93,6 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe @Override public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { - Toast.makeText(this.getActivity(),"Options created",Toast.LENGTH_SHORT).show(); inflater.inflate(R.menu.log_display, menu); super.onCreateOptionsMenu(menu, inflater); @@ -123,24 +115,64 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe } private void writeToLogFile(final OperationResult.OperationLog operationLog, final File f) { + OperationResult.OperationLog currLog = new OperationResult.OperationLog(); + currLog.add(OperationResult.LogType.MSG_EXPORT_LOG,0); + + boolean error = false; PrintWriter pw = null; - try { - pw = new PrintWriter(f); - } catch(IOException e) { - e.printStackTrace(); - } Iterator logIterator = operationLog.iterator(); + try { + pw = new PrintWriter(f); - while(logIterator.hasNext()) { + while(logIterator.hasNext()) { - pw.println(getPrintableLogEntry(logIterator.next())); + pw.println(getPrintableLogEntry(logIterator.next())); + if(pw.checkError()) {//IOException + Log.e(Constants.TAG, "Log Export I/O Exception "+f.getAbsolutePath()); + currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_WRITING,1); + error = true; + break; + } + } + } catch(FileNotFoundException e) { + Log.e(Constants.TAG, "File not found for exporting log "+f.getAbsolutePath()); + currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_FOPEN, 1); + error = true; } + if(pw!=null) { + pw.close(); + error = error && pw.checkError();//check for errors on closing PrintWriter object too + } + + if(!error) currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_SUCCESS,1); - pw.close(); + int opResultCode = error?OperationResult.RESULT_ERROR:OperationResult.RESULT_OK; + OperationResult opResult = new LogExportResult(opResultCode,currLog); + 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 CREATOR = new Creator() { + 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 a LogEntryParcel * @param entry log entry whose String representation is to be obtained @@ -162,7 +194,7 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe case DEBUG: subLogText+="[DEBUG]"; break; case INFO: subLogText+="[INFO]"; break; case WARN: subLogText+="[WARN]"; break; - case ERROR: subLogText+="[WARN]"; break; + case ERROR: subLogText+="[ERROR]"; break; case START: subLogText+="[START]"; break; case OK: subLogText+="[OK]"; break; case CANCELLED: subLogText+="[CANCELLED]"; break; @@ -192,7 +224,7 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe case DEBUG: logText="[DEBUG]"; break; case INFO: logText="[INFO]"; break; case WARN: logText="[WARN]"; break; - case ERROR: logText="[WARN]"; break; + case ERROR: logText="[ERROR]"; break; case START: logText="[START]"; break; case OK: logText="[OK]"; break; case CANCELLED: logText="[CANCELLED]"; break; diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 917c64bda..ed1dd1233 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -1168,6 +1168,13 @@ "An error occurred when searching for keys." + + "Exporting log" + "Error opening file" + "No file name specified!" + "I/O error writing to file!" + "Log exported successfully!" + "Click to clear cached passphrases" "OpenKeychain has cached %d passphrases" -- cgit v1.2.3 From e69ee812d2a73ef6d4a1c2df3e180bb61fa050e3 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Sun, 1 Mar 2015 11:31:46 +0530 Subject: supports sub logs in log export --- .../keychain/ui/LogDisplayFragment.java | 123 +++++++++------------ 1 file changed, 55 insertions(+), 68 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java index bd77294c1..ad5f9d53c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java @@ -121,21 +121,14 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe boolean error = false; PrintWriter pw = null; - - Iterator logIterator = operationLog.iterator(); try { pw = new PrintWriter(f); - - while(logIterator.hasNext()) { - - pw.println(getPrintableLogEntry(logIterator.next())); + pw.print(getPrintableOperationLog(operationLog,"")); if(pw.checkError()) {//IOException Log.e(Constants.TAG, "Log Export I/O Exception "+f.getAbsolutePath()); currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_WRITING,1); error = true; - break; } - } } catch(FileNotFoundException e) { Log.e(Constants.TAG, "File not found for exporting log "+f.getAbsolutePath()); currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_FOPEN, 1); @@ -143,7 +136,10 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe } if(pw!=null) { pw.close(); - error = error && pw.checkError();//check for errors on closing PrintWriter object too + 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); + error = true; + } } if(!error) currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_SUCCESS,1); @@ -173,78 +169,69 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe } }; } + /** - * returns an indented String of a LogEntryParcel - * @param entry log entry whose String representation is to be obtained - * @return the passed log entry in a readable format + * returns an indented String of an entire OperationLog + * @param operationLog 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 + * @return printable, indented version of passed operationLog */ - private String getPrintableLogEntry(OperationResult.LogEntryParcel entry) { - String subLogText=null; - if (entry instanceof SubLogEntryParcel) { - - OperationResult result = ((SubLogEntryParcel) entry).getSubResult(); - LogEntryParcel subEntry = result.getLog().getLast(); - if (subEntry != null) { - String subPadding=""; - for(int i=0;i 0 - && subEntry.mParameters[0] instanceof Integer) { - subLogText+=getResources().getQuantityString(subEntry.mType.getMsgId(), - (Integer) subEntry.mParameters[0], - subEntry.mParameters); - } else { - subLogText+=getResources().getString(subEntry.mType.getMsgId(), - subEntry.mParameters); - } - } - + private String getPrintableOperationLog(OperationResult.OperationLog operationLog, String basePadding) { + String log = ""; + Iterator logIterator = operationLog.iterator(); + while(logIterator.hasNext()) { + log += getPrintableLogEntry(logIterator.next(), basePadding)+"\n"; } + log = log.substring(0,log.length()-1);//gets rid of extra new line + return log; + } + /** + * 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 + * @return indented version of passed log entryParcel in a readable format + */ + private String getPrintableLogEntry(OperationResult.LogEntryParcel entryParcel, + String basePadding) { String logText = ""; + final String indent = " ";//4 spaces = 1 Indent level - String padding = ""; - for(int i =0;i 0 - && entry.mParameters[0] instanceof Integer) { - logText += getResources().getQuantityString(entry.mType.getMsgId(), - (Integer) entry.mParameters[0], - entry.mParameters); + if (entryParcel.mParameters != null && entryParcel.mParameters.length > 0 + && entryParcel.mParameters[0] instanceof Integer) { + logText += getResources().getQuantityString(entryParcel.mType.getMsgId(), + (Integer) entryParcel.mParameters[0], + entryParcel.mParameters); } else { - logText += getResources().getString(entry.mType.getMsgId(), - entry.mParameters); + logText += getResources().getString(entryParcel.mType.getMsgId(), + entryParcel.mParameters); } - logText = padding + logText; + if (entryParcel instanceof SubLogEntryParcel) { - if(subLogText!=null) //subLog exists - logText = logText+"\n"+padding+subLogText; + OperationResult subResult = ((SubLogEntryParcel) entryParcel).getSubResult(); + LogEntryParcel subEntry = subResult.getLog().getLast(); + if (subEntry != null) { + //the first line of log of subResult is same as entryParcel, so replace logText + logText = getPrintableOperationLog(subResult.getLog(),padding); + } + } return logText; } @@ -300,7 +287,7 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe mSecondImg = secondImg; } } - //adithyaphilip: Check if convertView.setPadding is redundant + // Check if convertView.setPadding is redundant @Override public View getView(int position, View convertView, ViewGroup parent) { LogEntryParcel entry = getItem(position); -- cgit v1.2.3 From 024ba19499f6975e2e153ca496d94cb5124f5dd8 Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Wed, 4 Mar 2015 04:14:28 +0530 Subject: corrected code style --- .../keychain/ui/LogDisplayFragment.java | 133 ++++++++++++--------- 1 file changed, 75 insertions(+), 58 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java index ad5f9d53c..138f2f4e7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java @@ -100,7 +100,7 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe @Override public boolean onOptionsItemSelected(MenuItem item) { - switch(item.getItemId()) { + switch (item.getItemId()) { case R.id.menu_log_display_export_log: exportLog(); break; @@ -116,100 +116,94 @@ public class LogDisplayFragment extends ListFragment implements OnItemClickListe private void writeToLogFile(final OperationResult.OperationLog operationLog, final File f) { 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; PrintWriter pw = null; try { - pw = new PrintWriter(f); - pw.print(getPrintableOperationLog(operationLog,"")); - if(pw.checkError()) {//IOException - Log.e(Constants.TAG, "Log Export I/O Exception "+f.getAbsolutePath()); - currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_WRITING,1); - error = true; - } - } catch(FileNotFoundException e) { - Log.e(Constants.TAG, "File not found for exporting log "+f.getAbsolutePath()); + pw = new PrintWriter(f); + pw.print(getPrintableOperationLog(operationLog, "")); + if (pw.checkError()) {//IOException + Log.e(Constants.TAG, "Log Export I/O Exception " + f.getAbsolutePath()); + currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_WRITING, 1); + error = true; + } + } catch (FileNotFoundException e) { + Log.e(Constants.TAG, "File not found for exporting log " + f.getAbsolutePath()); currLog.add(OperationResult.LogType.MSG_EXPORT_LOG_EXPORT_ERROR_FOPEN, 1); error = true; } - if(pw!=null) { + if (pw != null) { pw.close(); - 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); + 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); 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; - OperationResult opResult = new LogExportResult(opResultCode,currLog); + int opResultCode = error ? OperationResult.RESULT_ERROR : OperationResult.RESULT_OK; + OperationResult opResult = new LogExportResult(opResultCode, currLog); 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 CREATOR = new Creator() { - 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 - * @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 * @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 = ""; - Iterator logIterator = operationLog.iterator(); - while(logIterator.hasNext()) { - log += getPrintableLogEntry(logIterator.next(), basePadding)+"\n"; + for (Iterator logIterator = opLog.iterator(); logIterator.hasNext(); ) { + 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; } + /** * 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 * @return indented version of passed log entryParcel in a readable format */ private String getPrintableLogEntry(OperationResult.LogEntryParcel entryParcel, String basePadding) { - String logText = ""; final String indent = " ";//4 spaces = 1 Indent level String padding = basePadding; - for(int i =0;i CREATOR = new Creator() { + 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 public void onItemClick(AdapterView parent, View view, int position, long id) { LogEntryParcel parcel = mAdapter.getItem(position); -- cgit v1.2.3