aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java
blob: 94d9a60c9db21867ea0db5eb5ca12dcb0a8174f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
 * Copyright (C) 2014 Vincent Breitmoser <v.breitmoser@mugenguild.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.sufficientlysecure.keychain.ui;

import android.content.Context;
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;
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 {

    LogAdapter mAdapter;

    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) {
        super.onActivityCreated(savedInstanceState);

        Intent intent = getActivity().getIntent();
        if (intent == null) {
            getActivity().finish();
            return;
        }

        mResult = intent.getParcelableExtra(EXTRA_RESULT);
        if (mResult == null) {
            getActivity().finish();
            return;
        }

        mAdapter = new LogAdapter(getActivity(), mResult.getLog());
        setListAdapter(mAdapter);

        getListView().setOnItemClickListener(this);

        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<LogEntryParcel> 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 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<entry.mIndent;i++) subPadding+="  ";//2 spaces = 1 Indent

                subLogText=subPadding+"[SUB]";
                
                switch (subEntry.mType.mLevel) {
                    case DEBUG: subLogText+="[DEBUG]"; break;
                    case INFO: subLogText+="[INFO]"; break;
                    case WARN: subLogText+="[WARN]"; break;
                    case ERROR: subLogText+="[WARN]"; break;
                    case START: subLogText+="[START]"; break;
                    case OK: subLogText+="[OK]"; break;
                    case CANCELLED: subLogText+="[CANCELLED]"; break;
                }
                // special case: first parameter may be a quantity
                if (subEntry.mParameters != null && subEntry.mParameters.length > 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;
        }

        // 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<entry.mIndent;i++) {
            padding +="    "; //4 spaces = 1 Indent level
        }
        logText = padding + logText;

        if(subLogText!=null) //subLog exists
            logText = logText+"\n"+padding+subLogText;

        return logText;
    }

    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
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        LogEntryParcel parcel = mAdapter.getItem(position);
        if ( ! (parcel instanceof SubLogEntryParcel)) {
            return;
        }
        Intent intent = new Intent(
                getActivity(), LogDisplayActivity.class);
        intent.putExtra(LogDisplayFragment.EXTRA_RESULT, ((SubLogEntryParcel) parcel).getSubResult());
        startActivity(intent);
    }

    private class LogAdapter extends ArrayAdapter<LogEntryParcel> {

        private LayoutInflater mInflater;
        private int dipFactor;

        public LogAdapter(Context context, OperationResult.OperationLog log) {
            super(context, R.layout.log_display_item, log.toList());
            mInflater = LayoutInflater.from(getContext());
            dipFactor = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                    (float) 8, getResources().getDisplayMetrics());
        }

        private class ItemHolder {
            final View mSecond;
            final TextView mText, mSecondText;
            final ImageView mImg, mSecondImg, mSub;
            public ItemHolder(TextView text, ImageView image, ImageView sub, View second, TextView secondText, ImageView secondImg) {
                mText = text;
                mImg = image;
                mSub = sub;
                mSecond = second;
                mSecondText = secondText;
                mSecondImg = secondImg;
            }
        }
        //adithyaphilip: Check if convertView.setPadding is redundant
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LogEntryParcel entry = getItem(position);
            ItemHolder ih;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.log_display_item, parent, false);
                ih = new ItemHolder(
                        (TextView) convertView.findViewById(R.id.log_text),
                        (ImageView) convertView.findViewById(R.id.log_img),
                        (ImageView) convertView.findViewById(R.id.log_sub),
                        convertView.findViewById(R.id.log_second),
                        (TextView) convertView.findViewById(R.id.log_second_text),
                        (ImageView) convertView.findViewById(R.id.log_second_img)
                );
                convertView.setTag(ih);
            } else {
                ih = (ItemHolder) convertView.getTag();
            }

            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();
                LogEntryParcel subEntry = result.getLog().getLast();
                if (subEntry != null) {
                    ih.mSecond.setVisibility(View.VISIBLE);
                    // special case: first parameter may be a quantity
                    if (subEntry.mParameters != null && subEntry.mParameters.length > 0
                            && subEntry.mParameters[0] instanceof Integer) {
                        ih.mSecondText.setText(getResources().getQuantityString(subEntry.mType.getMsgId(),
                                (Integer) subEntry.mParameters[0],
                                subEntry.mParameters));
                    } else {
                        ih.mSecondText.setText(getResources().getString(subEntry.mType.getMsgId(),
                                subEntry.mParameters));
                    }
                    ih.mSecondText.setTextColor(subEntry.mType.mLevel == LogLevel.DEBUG ? Color.GRAY : Color.BLACK);
                    switch (subEntry.mType.mLevel) {
                        case DEBUG: ih.mSecondImg.setBackgroundColor(Color.GRAY); break;
                        case INFO: ih.mSecondImg.setBackgroundColor(Color.BLACK); break;
                        case WARN: ih.mSecondImg.setBackgroundColor(getResources().getColor(R.color.android_orange_light)); break;
                        case ERROR: ih.mSecondImg.setBackgroundColor(getResources().getColor(R.color.android_red_light)); break;
                        case START: ih.mSecondImg.setBackgroundColor(Color.BLACK); break;
                        case OK: ih.mSecondImg.setBackgroundColor(getResources().getColor(R.color.android_green_light)); break;
                        case CANCELLED: ih.mSecondImg.setBackgroundColor(getResources().getColor(R.color.android_red_light)); break;
                    }
                } else {
                    ih.mSecond.setVisibility(View.GONE);
                }

            } else {
                Log.d("ADI DisplayFragment","NOT:"+entry.toString());
                ih.mSub.setVisibility(View.GONE);
                ih.mSecond.setVisibility(View.GONE);
                convertView.setClickable(true);
            }

            // special case: first parameter may be a quantity
            if (entry.mParameters != null && entry.mParameters.length > 0
                    && entry.mParameters[0] instanceof Integer) {
                ih.mText.setText(getResources().getQuantityString(entry.mType.getMsgId(),
                        (Integer) entry.mParameters[0],
                        entry.mParameters));
            } else {
                ih.mText.setText(getResources().getString(entry.mType.getMsgId(),
                        entry.mParameters));
            }
            convertView.setPadding((entry.mIndent) * dipFactor, 0, 0, 0);
            ih.mText.setTextColor(entry.mType.mLevel == LogLevel.DEBUG ? Color.GRAY : Color.BLACK);
            switch (entry.mType.mLevel) {
                case DEBUG: ih.mImg.setBackgroundColor(Color.GRAY); break;
                case INFO: ih.mImg.setBackgroundColor(Color.BLACK); break;
                case WARN: ih.mImg.setBackgroundColor(getResources().getColor(R.color.android_orange_light)); break;
                case ERROR: ih.mImg.setBackgroundColor(getResources().getColor(R.color.android_red_light)); break;
                case START: ih.mImg.setBackgroundColor(Color.BLACK); break;
                case OK: ih.mImg.setBackgroundColor(getResources().getColor(R.color.android_green_light)); break;
                case CANCELLED: ih.mImg.setBackgroundColor(getResources().getColor(R.color.android_red_light)); break;
            }

            return convertView;
        }

    }
}