1
+ package io .github .tscholze .firebasepager ;
2
+
3
+ import android .app .Activity ;
4
+ import android .content .BroadcastReceiver ;
5
+ import android .content .Context ;
6
+ import android .content .Intent ;
7
+ import android .content .IntentFilter ;
8
+ import android .os .Bundle ;
9
+
10
+ import java .io .IOException ;
11
+ import java .util .LinkedList ;
12
+ import java .util .List ;
13
+
14
+ import android .os .Handler ;
15
+ import android .util .Log ;
16
+
17
+ import com .google .android .things .contrib .driver .ht16k33 .AlphanumericDisplay ;
18
+ import com .google .firebase .iid .FirebaseInstanceId ;
19
+ import com .google .firebase .messaging .FirebaseMessaging ;
20
+
21
+ /**
22
+ * This activity is responsible to display messages from Firebase
23
+ * Cloud Messaging notifications on an alphanumerical display.
24
+ */
25
+ public class MainActivity extends Activity implements IRunningTextContext
26
+ {
27
+ private static final String TAG = MainActivity .class .getSimpleName ();
28
+ private static final String TOPIC_IDENTIFIER = "messages" ;
29
+ private static final String I2C1_BUS_IDENTIFIER = "I2C1" ;
30
+
31
+ private AlphanumericDisplay alphanumericDisplay ;
32
+ private List <String > messages = new LinkedList <>();
33
+ private Handler runningTextHandler ;
34
+
35
+ private BroadcastReceiver broadcastReceiver = new BroadcastReceiver ()
36
+ {
37
+ @ Override
38
+ public void onReceive (Context context , Intent intent )
39
+ {
40
+ messages .add (intent .getStringExtra (MyFirebaseMessagingService .EXTRA_BODY ));
41
+
42
+ if (messages .size () == 1 )
43
+ {
44
+ postTextToDisplay ();
45
+ }
46
+ }
47
+ };
48
+
49
+ @ Override
50
+ protected void onCreate (Bundle savedInstanceState )
51
+ {
52
+ super .onCreate (savedInstanceState );
53
+
54
+ FirebaseMessaging .getInstance ().subscribeToTopic (TOPIC_IDENTIFIER );
55
+ Log .d (TAG , "FCM token: " + FirebaseInstanceId .getInstance ().getToken ());
56
+
57
+ setupAlphanumericDisplay ();
58
+ runningTextHandler = new Handler ();
59
+ IntentFilter intentFilter = new IntentFilter (MyFirebaseMessagingService .NEW_MESSAGE_ITENT );
60
+ registerReceiver (broadcastReceiver , intentFilter );
61
+
62
+ messages .add ("Teeeeeest" );
63
+ postTextToDisplay ();
64
+ }
65
+
66
+ @ Override
67
+ protected void onDestroy ()
68
+ {
69
+ super .onDestroy ();
70
+
71
+ destroyAlphanumericDisplay ();
72
+ FirebaseMessaging .getInstance ().unsubscribeFromTopic (TOPIC_IDENTIFIER );
73
+ }
74
+
75
+ @ Override
76
+ public AlphanumericDisplay getAlphaNumericDisplay ()
77
+ {
78
+ return alphanumericDisplay ;
79
+ }
80
+
81
+ @ Override
82
+ public Handler getRunningTextHandler ()
83
+ {
84
+ return runningTextHandler ;
85
+ }
86
+
87
+ @ Override
88
+ public void onRunningTextFinished ()
89
+ {
90
+ Log .d (TAG , "Running text finished" );
91
+ postTextToDisplay ();
92
+ }
93
+
94
+ private void setupAlphanumericDisplay ()
95
+ {
96
+ try
97
+ {
98
+ alphanumericDisplay = new AlphanumericDisplay (I2C1_BUS_IDENTIFIER );
99
+ alphanumericDisplay .setBrightness (1.0f );
100
+ alphanumericDisplay .setEnabled (true );
101
+ alphanumericDisplay .clear ();
102
+ }
103
+ catch (IOException e )
104
+ {
105
+ Log .e (TAG , "Error configuring display" , e );
106
+ }
107
+ }
108
+
109
+ private void destroyAlphanumericDisplay ()
110
+ {
111
+ if (alphanumericDisplay != null )
112
+ {
113
+ Log .i (TAG , "Closing display" );
114
+ try
115
+ {
116
+ alphanumericDisplay .close ();
117
+ }
118
+ catch (IOException e )
119
+ {
120
+ Log .e (TAG , "Error closing display" , e );
121
+ }
122
+ finally
123
+ {
124
+ alphanumericDisplay = null ;
125
+ }
126
+ }
127
+ }
128
+
129
+ private void postTextToDisplay ()
130
+ {
131
+ if (messages .isEmpty ())
132
+ {
133
+ return ;
134
+ }
135
+
136
+ String message = messages .get (0 );
137
+ Log .d (TAG , "Will post new message: " + message );
138
+
139
+ RunningTextRunnable runnable = new RunningTextRunnable (this , message );
140
+ messages .remove (0 );
141
+ runningTextHandler .post (runnable );
142
+ }
143
+ }
0 commit comments