366a5ab6f25dc4fd3cfe3c72dd8d9ac1659ba9c8
[goodguy/cinelerra.git] / CineRmt / src / main / java / com / local / cinermt / MainActivity.java
1 package com.local.cinermt;
2
3 import android.content.Context;
4 import android.content.Intent;
5 import android.content.SharedPreferences;
6 import android.net.ConnectivityManager;
7 import android.net.NetworkInfo;
8 import android.os.Bundle;
9 import android.os.Handler;
10 import android.os.SystemClock;
11 import android.support.v7.app.AppCompatActivity;
12 import android.view.Menu;
13 import android.view.MenuItem;
14 import android.view.View;
15 import android.view.View.OnClickListener;
16 import android.widget.ImageButton;
17 import android.widget.Toast;
18
19 import java.net.DatagramPacket;
20 import java.net.DatagramSocket;
21 import java.net.InetAddress;
22
23 //public class MainActivity extends Activity 
24 public class MainActivity extends AppCompatActivity
25                 implements OnClickListener {
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             ConnectivityManager cmgr = (ConnectivityManager)
68                     getSystemService(Context.CONNECTIVITY_SERVICE);
69             NetworkInfo netinf =
70                     cmgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
71             netinf = cmgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
72             if( netinf == null ) return false;
73             if(!netinf.isAvailable()) return false;
74             if(!netinf.isConnected()) return false;
75             return true;
76         }
77         
78         boolean create_socket() {
79                 if(socket != null) socket.close();
80                 socket = null;
81                 int retry = 10;
82                 while( --retry >= 0 ) {
83                         sport = (int)(Math.random() * (65536-1024)) + 1024;
84                         try {
85                                 in_adr = InetAddress.getAllByName(ip_addr)[0];
86                                 socket = new DatagramSocket(sport);
87                                 socket.setBroadcast(true);
88                         } catch(Exception e) { SystemClock.sleep(100);  continue; }
89                         break;
90                 }
91                 return retry >= 0;
92         }
93
94         void save_defaults() {
95                 SharedPreferences.Editor ed = prefs.edit();
96                 ed.putString("IP_ADDR", ip_addr);
97                 ed.putString("PIN", pin);
98                 ed.putInt("PORT", dport);
99         ed.commit();
100         }
101         
102         void load_defaults() {
103                 ip_addr = prefs.getString("IP_ADDR", IP_ADDR);
104                 pin = prefs.getString("PIN", PIN);
105                 dport = prefs.getInt("PORT", DPORT);
106         }
107         
108         @Override
109         protected void onCreate(Bundle b) {
110                 super.onCreate(b);
111                 setContentView(R.layout.activity_main);
112                 if( !has_network() ) {
113                         Toast.makeText(this, "Can't access wifi", Toast.LENGTH_LONG).show();
114                         SystemClock.sleep(5000);
115                         finish();
116                 }
117         prefs = this.getSharedPreferences("CineRmt", 0);
118                 load_defaults();
119                 Intent it = getIntent();
120         String s = it.getStringExtra("IP_ADDR");
121         if( s != null ) ip_addr = s;
122         s = it.getStringExtra("PIN");
123         if( s != null ) pin = s;
124         dport = it.getIntExtra("PORT", dport);
125         if( dport < 1024 ) dport = DPORT;
126                 if( !create_socket() ) {
127                         Toast.makeText(this, "Can't access network", Toast.LENGTH_LONG).show();
128                         SystemClock.sleep(5000);
129                 }
130                 ImageButton img = (ImageButton)findViewById(R.id.button0);
131                 img.setOnClickListener(this);
132                 img = (ImageButton)findViewById(R.id.button1);
133                 img.setOnClickListener(this);
134                 img = (ImageButton)findViewById(R.id.button2);
135                 img.setOnClickListener(this);
136                 img = (ImageButton)findViewById(R.id.button3);
137                 img.setOnClickListener(this);
138                 img = (ImageButton)findViewById(R.id.button4);
139                 img.setOnClickListener(this);
140                 img = (ImageButton)findViewById(R.id.button5);
141                 img.setOnClickListener(this);
142                 img = (ImageButton)findViewById(R.id.button6);
143                 img.setOnClickListener(this);
144                 img = (ImageButton)findViewById(R.id.button7);
145                 img.setOnClickListener(this);
146                 img = (ImageButton)findViewById(R.id.button8);
147                 img.setOnClickListener(this);
148                 img = (ImageButton)findViewById(R.id.button9);
149                 img.setOnClickListener(this);
150                 img = (ImageButton)findViewById(R.id.buttonA);
151                 img.setOnClickListener(this);
152                 img = (ImageButton)findViewById(R.id.buttonB);
153                 img.setOnClickListener(this);
154                 img = (ImageButton)findViewById(R.id.buttonC);
155                 img.setOnClickListener(this);
156                 img = (ImageButton)findViewById(R.id.buttonD);
157                 img.setOnClickListener(this);
158                 img = (ImageButton)findViewById(R.id.buttonE);
159                 img.setOnClickListener(this);
160                 img = (ImageButton)findViewById(R.id.buttonF);
161                 img.setOnClickListener(this);
162                 img = (ImageButton)findViewById(R.id.button_dot);
163                 img.setOnClickListener(this);
164                 img = (ImageButton)findViewById(R.id.fast_lt);
165                 img.setOnClickListener(this);
166                 img = (ImageButton)findViewById(R.id.media_up);
167                 img.setOnClickListener(this);
168                 img = (ImageButton)findViewById(R.id.fast_rt);
169                 img.setOnClickListener(this);
170                 img = (ImageButton)findViewById(R.id.menu);
171                 img.setOnClickListener(this);
172                 img = (ImageButton)findViewById(R.id.media_lt);
173                 img.setOnClickListener(this);
174                 img = (ImageButton)findViewById(R.id.pause);
175                 img.setOnClickListener(this);
176                 img = (ImageButton)findViewById(R.id.media_rt);
177                 img.setOnClickListener(this);
178                 img = (ImageButton)findViewById(R.id.slow_lt);
179                 img.setOnClickListener(this);
180                 img = (ImageButton)findViewById(R.id.media_dn);
181                 img.setOnClickListener(this);
182                 img = (ImageButton)findViewById(R.id.slow_rt);
183                 img.setOnClickListener(this);
184                 img = (ImageButton)findViewById(R.id.full_scr);
185                 img.setOnClickListener(this);
186                 img = (ImageButton)findViewById(R.id.stop);
187                 img.setOnClickListener(this);
188                 img = (ImageButton)findViewById(R.id.play);
189                 img.setOnClickListener(this);
190                 img = (ImageButton)findViewById(R.id.rplay);
191                 img.setOnClickListener(this);
192
193                 img = (ImageButton)findViewById(R.id.suspend);
194                 img.setOnClickListener(this);
195                 img = (ImageButton)findViewById(R.id.config);
196                 img.setOnClickListener(this);
197                 img = (ImageButton)findViewById(R.id.exit);
198                 img.setOnClickListener(this);
199                 img = (ImageButton)findViewById(R.id.power);
200                 img.setOnClickListener(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.menu_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.stop) { send("stop"); }
215                         else if (id == R.id.play) { send("play"); }
216                         else if (id == R.id.rplay) { send("rplay"); }
217                         else if (id == R.id.button0) { send("key 0"); }
218                         else if (id == R.id.button1) { send("key 1"); }
219                         else if (id == R.id.button2) { send("key 2"); }
220                         else if (id == R.id.button3) { send("key 3"); }
221                         else if (id == R.id.button4) { send("key 4"); }
222                         else if (id == R.id.button5) { send("key 5"); }
223                         else if (id == R.id.button6) { send("key 6"); }
224                         else if (id == R.id.button7) { send("key 7"); }
225                         else if (id == R.id.button8) { send("key 8"); }
226                         else if (id == R.id.button9) { send("key 9"); }
227                         else if (id == R.id.buttonA) { send("key A"); }
228                         else if (id == R.id.buttonB) { send("key B"); }
229                         else if (id == R.id.buttonC) { send("key C"); }
230                         else if (id == R.id.buttonD) { send("key D"); }
231                         else if (id == R.id.buttonE) { send("key E"); }
232                         else if (id == R.id.buttonF) { send("key F"); }
233                         else if (id == R.id.fast_lt) { send("fast_lt"); }
234                         else if (id == R.id.media_up) { send("media_up"); }
235                         else if (id == R.id.fast_rt) { send("fast_rt"); }
236                         else if (id == R.id.menu) { send("menu"); }
237                         else if (id == R.id.media_lt) { send("media_lt"); }
238                         else if (id == R.id.pause) { send("pause"); }
239                         else if (id == R.id.media_rt) { send("media_rt"); }
240                         else if (id == R.id.slow_lt) { send("slow_lt"); }
241                         else if (id == R.id.media_dn) { send("media_dn"); }
242                         else if (id == R.id.slow_rt) { send("slow_rt"); }
243                         else if (id == R.id.full_scr) { send("key F"); }
244                         else {
245                                 save_defaults();
246                                 if (id == R.id.config) {
247                                         Intent it = new Intent(this, ConfigActivity.class);
248                                         it.putExtra("IP_ADDR", ip_addr);
249                                         it.putExtra("PIN", pin);
250                                         it.putExtra("PORT", dport);
251                                         startActivity(it);
252                                 }
253                                 else if (id == R.id.suspend)
254                                         send("suspend");
255                                 else if (id == R.id.power)
256                                         send("power");
257                                 else if (id != R.id.exit)
258                                         return;
259                                 finish();
260                         }
261                 }
262         }
263
264         @Override
265         public boolean onOptionsItemSelected(MenuItem item) {
266                 super.onOptionsItemSelected(item);
267                 int id = item.getItemId();
268                 if (id == R.id.menu_exit) {
269                         save_defaults();
270                         finish();
271                         return true;
272                 }
273                 return false;
274         }
275         
276         @Override
277         public void onPause() {
278                 super.onPause();
279                 save_defaults();
280         }
281         
282         @Override
283         public void onDestroy() {
284                 super.onDestroy();
285                 save_defaults();
286         }
287 }