initial commit
[goodguy/history.git] / CineRmt / src / com / local / cinermt / MainActivity.java
1 package com.local.cinermt;
2
3 import java.net.DatagramPacket;
4 import java.net.DatagramSocket;
5 import java.net.InetAddress;
6
7 import android.app.Activity;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.SharedPreferences;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.view.View.OnLongClickListener;
14 import android.net.ConnectivityManager;
15 import android.net.NetworkInfo;
16 import android.os.Bundle;
17 import android.os.Handler;
18 import android.os.SystemClock;
19 import android.view.Menu;
20 import android.view.MenuItem;
21 import android.widget.ImageButton;
22 import android.widget.Toast;
23
24 public class MainActivity extends Activity 
25                 implements OnClickListener, OnLongClickListener {
26         String ip_addr;
27         int sport, dport;
28         String pin;
29         
30         private final String IP_ADDR = "127.0.0.1";
31         private final int DPORT = 23432;
32         private final String MAGIC = "CAR";
33         private final String PIN = "cinelerra";
34         private final char VER = '\001', ZERO = '\000';
35         
36     private SharedPreferences prefs;
37     
38         InetAddress in_adr;
39         DatagramSocket socket;
40         Handler hndr = new Handler();
41         
42         
43         class sender extends Thread implements Runnable {
44                 public String data;
45                 sender(String d) { data = d; }
46                 
47                 @Override
48             public void run() {
49                         if( socket == null ) return;
50                         String buf = MAGIC + VER + pin + ZERO + data;
51                         try {
52                                 DatagramPacket packet =
53                                         new DatagramPacket(buf.getBytes(), buf.length(), in_adr, dport);
54                                 socket.send(packet);
55                         } catch(Exception e) {
56                                 return;
57                         }
58                 }
59         }
60
61         public void send(String data) {
62                 final Thread net = new sender(data);
63                 net.start();
64         }
65         
66         boolean has_network()
67         {
68             ConnectivityManager cmgr = (ConnectivityManager)
69                     getSystemService(Context.CONNECTIVITY_SERVICE);
70             NetworkInfo netinf =
71                     cmgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
72             netinf = cmgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
73             if( netinf == null ) return false;
74             if(!netinf.isAvailable()) return false;
75             if(!netinf.isConnected()) return false;
76             return true;
77         }
78         
79         boolean create_socket()
80         {
81                 if(socket != null) socket.close();
82                 socket = null;
83                 int retry = 10;
84                 while( --retry >= 0 ) {
85                         sport = (int)(Math.random() * (65536-1024)) + 1024;
86                         try {
87                                 in_adr = InetAddress.getAllByName(ip_addr)[0];
88                                 socket = new DatagramSocket(sport);
89                                 socket.setBroadcast(true);
90                         } catch(Exception e) { SystemClock.sleep(100);  continue; }
91                         break;
92                 }
93                 return retry >= 0;
94         }
95
96         void save_defaults()
97         {
98                 SharedPreferences.Editor ed = prefs.edit();
99                 ed.putString("IP_ADDR", ip_addr);
100                 ed.putString("PIN", pin);
101                 ed.putInt("PORT", dport);
102         ed.commit();
103         }
104         
105         void load_defaults()
106         {
107                 ip_addr = prefs.getString("IP_ADDR", IP_ADDR);
108                 pin = prefs.getString("PIN", PIN);
109                 dport = prefs.getInt("PORT", DPORT);
110         }
111         
112         @Override
113         protected void onCreate(Bundle b) {
114                 super.onCreate(b);
115                 setContentView(R.layout.activity_main);
116                 if( !has_network() ) {
117                         Toast.makeText(this, "Can't access wifi", Toast.LENGTH_LONG).show();
118                         SystemClock.sleep(5000);
119                         finish();
120                 }
121         prefs = this.getSharedPreferences("CineRmt", 0);
122                 load_defaults();
123                 Intent it = getIntent();
124         String s = it.getStringExtra("IP_ADDR");
125         if( s != null ) ip_addr = s;
126         s = it.getStringExtra("PIN");
127         if( s != null ) pin = s;
128         dport = it.getIntExtra("PORT", dport);
129         if( dport < 1024 ) dport = DPORT;
130                 if( !create_socket() ) {
131                         Toast.makeText(this, "Can't access network", Toast.LENGTH_LONG).show();
132                         SystemClock.sleep(5000);
133                 }
134                 ImageButton img = (ImageButton)findViewById(R.id.button0);
135                 img.setOnClickListener(this);
136                 img = (ImageButton)findViewById(R.id.button1);
137                 img.setOnClickListener(this);
138                 img = (ImageButton)findViewById(R.id.button2);
139                 img.setOnClickListener(this);
140                 img = (ImageButton)findViewById(R.id.button3);
141                 img.setOnClickListener(this);
142                 img = (ImageButton)findViewById(R.id.button4);
143                 img.setOnClickListener(this);
144                 img = (ImageButton)findViewById(R.id.button5);
145                 img.setOnClickListener(this);
146                 img = (ImageButton)findViewById(R.id.button6);
147                 img.setOnClickListener(this);
148                 img = (ImageButton)findViewById(R.id.button7);
149                 img.setOnClickListener(this);
150                 img = (ImageButton)findViewById(R.id.button8);
151                 img.setOnClickListener(this);
152                 img = (ImageButton)findViewById(R.id.button9);
153                 img.setOnClickListener(this);
154                 img = (ImageButton)findViewById(R.id.buttonA);
155                 img.setOnClickListener(this);
156                 img = (ImageButton)findViewById(R.id.buttonB);
157                 img.setOnClickListener(this);
158                 img = (ImageButton)findViewById(R.id.buttonC);
159                 img.setOnClickListener(this);
160                 img = (ImageButton)findViewById(R.id.buttonD);
161                 img.setOnClickListener(this);
162                 img = (ImageButton)findViewById(R.id.buttonE);
163                 img.setOnClickListener(this);
164                 img = (ImageButton)findViewById(R.id.buttonF);
165                 img.setOnClickListener(this);
166                 img = (ImageButton)findViewById(R.id.button_dot);
167                 img.setOnClickListener(this);
168                 img = (ImageButton)findViewById(R.id.fast_lt);
169                 img.setOnClickListener(this);
170                 img = (ImageButton)findViewById(R.id.media_up);
171                 img.setOnClickListener(this);
172                 img = (ImageButton)findViewById(R.id.fast_rt);
173                 img.setOnClickListener(this);
174                 img = (ImageButton)findViewById(R.id.menu);
175                 img.setOnClickListener(this);
176                 img = (ImageButton)findViewById(R.id.media_lt);
177                 img.setOnClickListener(this);
178                 img = (ImageButton)findViewById(R.id.pause);
179                 img.setOnClickListener(this);
180                 img = (ImageButton)findViewById(R.id.media_rt);
181                 img.setOnClickListener(this);
182                 img = (ImageButton)findViewById(R.id.slow_lt);
183                 img.setOnClickListener(this);
184                 img = (ImageButton)findViewById(R.id.media_dn);
185                 img.setOnClickListener(this);
186                 img = (ImageButton)findViewById(R.id.slow_rt);
187                 img.setOnClickListener(this);
188                 img = (ImageButton)findViewById(R.id.full_scr);
189                 img.setOnClickListener(this);
190                 img = (ImageButton)findViewById(R.id.stop);
191                 img.setOnClickListener(this);
192                 img = (ImageButton)findViewById(R.id.play);
193                 img.setOnClickListener(this);
194                 
195                 img = (ImageButton)findViewById(R.id.config);
196                 img.setOnLongClickListener(this);
197                 img = (ImageButton)findViewById(R.id.suspend);
198                 img.setOnLongClickListener(this);
199                 img = (ImageButton)findViewById(R.id.stop);
200                 img.setOnLongClickListener(this);
201         }
202
203         @Override
204         public boolean onCreateOptionsMenu(Menu menu) {
205                 // Inflate the menu; this adds items to the action bar if it is present.
206                 getMenuInflater().inflate(R.menu.activity_main, menu);
207                 return true;
208         }
209
210         @Override
211         public void onClick(View v) {
212         if (v instanceof ImageButton) {
213             int id = ((ImageButton)v).getId();
214                         if (id == R.id.button0) { send("key 0"); }
215                         else if (id == R.id.button1) { send("key 1"); }
216                         else if (id == R.id.button2) { send("key 2"); }
217                         else if (id == R.id.button3) { send("key 3"); }
218                         else if (id == R.id.button4) { send("key 4"); }
219                         else if (id == R.id.button5) { send("key 5"); }
220                         else if (id == R.id.button6) { send("key 6"); }
221                         else if (id == R.id.button7) { send("key 7"); }
222                         else if (id == R.id.button8) { send("key 8"); }
223                         else if (id == R.id.button9) { send("key 9"); }
224                         else if (id == R.id.buttonA) { send("key A"); }
225                         else if (id == R.id.buttonB) { send("key B"); }
226                         else if (id == R.id.buttonC) { send("key C"); }
227                         else if (id == R.id.buttonD) { send("key D"); }
228                         else if (id == R.id.buttonE) { send("key E"); }
229                         else if (id == R.id.buttonF) { send("key F"); }
230                         else if (id == R.id.fast_lt) { send("fast_lt"); }
231                         else if (id == R.id.media_up) { send("media_up"); }
232                         else if (id == R.id.fast_rt) { send("fast_rt"); }
233                         else if (id == R.id.menu) { send("menu"); }
234                         else if (id == R.id.media_lt) { send("media_lt"); }
235                         else if (id == R.id.pause) { send("pause"); }
236                         else if (id == R.id.media_rt) { send("media_rt"); }
237                         else if (id == R.id.slow_lt) { send("slow_lt"); }
238                         else if (id == R.id.media_dn) { send("media_dn"); }
239                         else if (id == R.id.slow_rt) { send("slow_rt"); }
240                         else if (id == R.id.full_scr) { send("key F"); }
241                         else if (id == R.id.stop) { send("stop"); }
242                         else if (id == R.id.play) { send("play"); }
243         }
244         }
245
246         @Override
247         public boolean onOptionsItemSelected(MenuItem item) {
248                 super.onOptionsItemSelected(item);
249                 int id = item.getItemId();
250                 if (id == R.id.menu_exit) {
251                         save_defaults();
252                         finish();
253                         return true;
254                 }
255                 return false;
256         }
257         
258         @Override
259         public void onPause()
260         {
261                 super.onPause();
262                 save_defaults();
263         }
264         
265         @Override
266         public void onDestroy()
267         {
268                 super.onDestroy();
269                 save_defaults();
270         }
271
272         @Override
273         public boolean onLongClick(View v) {
274         if (v instanceof ImageButton) {
275                         int id = ((ImageButton)v).getId();
276                         if (id == R.id.config) {
277                         Intent it = new Intent(this, ConfigActivity.class);
278                         it.putExtra("IP_ADDR", ip_addr);
279                         it.putExtra("PIN", pin);
280                         it.putExtra("PORT", dport);
281                         startActivity(it);
282                         finish();
283                                 return true;
284                         }
285                         if (id == R.id.suspend) {
286                                 send("suspend");
287                                 save_defaults();
288                                 finish();
289                                 return true;
290                         }
291                         if (id == R.id.stop) {
292                                 save_defaults();
293                                 finish();
294                                 return true;
295                         }
296         }
297         return false;
298         }
299 }