summaryrefslogtreecommitdiffstats
path: root/Android/06-Notifications/Notifications/src/course/labs/notificationslab/TestFrontEndActivity.java
blob: 72b1a6f6f1875ee27f747b49538c242195b1993e (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
package course.labs.notificationslab;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestFrontEndActivity extends Activity {

    private final static long DAWN_OF_TIME = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_front_end);

        Button ageTweetsButton = (Button) findViewById(R.id.age_tweets_button);
        ageTweetsButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setFileAge(DAWN_OF_TIME);
            }
        });

        Button rejuvTweetsButton = (Button) findViewById(R.id.rejuv_tweets_button);
        rejuvTweetsButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setFileAge(System.currentTimeMillis());
            }
        });

        Button startMainActivityButton = (Button) findViewById(R.id.start_main_button);
        startMainActivityButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TestFrontEndActivity.this,
                        MainActivity.class));
            }
        });
        createTweetFileIfMissing();
    }

    private void createTweetFileIfMissing() {

        String fname = TestFrontEndActivity.this.getFilesDir() + "/" + MainActivity.TWEET_FILENAME;

        File file = new File(fname);
        if (!file.exists()) {

            PrintWriter out = null;
            BufferedReader in = null;

            try {
                out = new PrintWriter(new BufferedWriter(
                            new OutputStreamWriter(openFileOutput(
                                    MainActivity.TWEET_FILENAME,
                                    Context.MODE_PRIVATE))));

                for (int resId : DownloaderTask.txtFeeds) {
                    in = new BufferedReader(new InputStreamReader(
                                getResources().openRawResource(resId)));

                    String line;
                    StringBuffer buffer = new StringBuffer();

                    while ((line = in.readLine()) != null) {
                        buffer.append(line);
                    }
                    out.println(buffer);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (Resources.NotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != in) {
                        in.close();
                    }
                    if (null != out) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void setFileAge(long timestamp) {
        String fname = TestFrontEndActivity.this.getFilesDir() + "/" + MainActivity.TWEET_FILENAME;
        File file = new File(fname);
        if (file.exists()) {
            file.setLastModified(timestamp);
        }
    }

}