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