1package com.android.bugreportsender;
2
3import android.app.Activity;
4import android.content.Intent;
5import android.net.Uri;
6import android.os.Bundle;
7import android.util.Log;
8import android.widget.TextView;
9
10import java.io.FileNotFoundException;
11import java.io.IOException;
12import java.io.InputStream;
13
14/**
15 * Provides a scrolling text view previewing a named section of a
16 * bugreport.
17 */
18public class BugReportPreviewActivity extends Activity {
19
20    private static final String TAG = "BugReportPreview";
21
22    private TextView mText;
23
24    @Override
25    protected void onCreate(Bundle icicle) {
26        super.onCreate(icicle);
27
28        setContentView(com.android.bugreportsender.R.layout.bugreport_preview);
29        mText = (TextView) findViewById(R.id.preview);
30
31        if (icicle != null && icicle.getString("text") != null) {
32            mText.setText(icicle.getString("text"));
33        } else {
34            Intent intent = getIntent();
35            if (intent == null) {
36                Log.w(TAG, "No intent provided.");
37                return;
38            }
39            Uri uri = intent.getData();
40            String section = intent.getStringExtra("section");
41            if (section == null || section.length() == 0) {
42                section = "SYSTEM LOG";
43            }
44            Log.d(TAG, "Loading " + uri);
45            InputStream in = null;
46            try {
47		// TODO: do this in a background thread, using handlers and all that nonsense.
48                in = getContentResolver().openInputStream(uri);
49                String text = BugReportParser.extractSystemLogs(in, section);
50                mText.setText(text);
51            } catch (FileNotFoundException fnfe) {
52                Log.w(TAG, "Unable to open file: " + uri, fnfe);
53		mText.setText("Unable to open file: " + uri + ": " + fnfe);
54                return;
55            } catch (IOException ioe) {
56                Log.w(TAG, "Unable to process log.", ioe);
57		mText.setText("Unable to process log: " + ioe);
58		return;
59            } finally {
60                try {
61                    if (in != null) {
62			in.close();
63		    }
64                } catch (IOException ioe) {
65                    // ignore
66                }
67            }
68        }
69    }
70
71    @Override
72    protected void onSaveInstanceState(Bundle icicle) {
73        CharSequence text = mText.getText();
74        if (text != null) {
75            icicle.putString("text", mText.getText().toString());
76        }
77    }
78}
79