1 package com.local.cinermt;
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.view.View.OnLongClickListener;
17 import android.widget.ImageButton;
18 import android.widget.Toast;
20 import java.net.DatagramPacket;
21 import java.net.DatagramSocket;
22 import java.net.InetAddress;
24 //public class MainActivity extends Activity
25 public class MainActivity extends AppCompatActivity
26 implements OnClickListener {
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';
37 private SharedPreferences prefs;
40 DatagramSocket socket;
41 Handler hndr = new Handler();
44 class sender extends Thread implements Runnable {
46 sender(String d) { data = d; }
50 if( socket == null ) return;
51 String buf = MAGIC + VER + pin + ZERO + data;
53 DatagramPacket packet =
54 new DatagramPacket(buf.getBytes(), buf.length(), in_adr, dport);
56 } catch(Exception e) {
62 public void send(String data) {
63 final Thread net = new sender(data);
67 boolean has_network() {
68 ConnectivityManager cmgr = (ConnectivityManager)
69 getSystemService(Context.CONNECTIVITY_SERVICE);
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;
79 boolean create_socket() {
80 if(socket != null) socket.close();
83 while( --retry >= 0 ) {
84 sport = (int)(Math.random() * (65536-1024)) + 1024;
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; }
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);
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);
110 protected void onCreate(Bundle 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);
118 prefs = this.getSharedPreferences("CineRmt", 0);
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);
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);
192 img = (ImageButton)findViewById(R.id.suspend);
193 img.setOnClickListener(this);
194 img = (ImageButton)findViewById(R.id.config);
195 img.setOnClickListener(this);
196 img = (ImageButton)findViewById(R.id.exit);
197 img.setOnClickListener(this);
198 img = (ImageButton)findViewById(R.id.power);
199 img.setOnClickListener(this);
203 public boolean onCreateOptionsMenu(Menu menu) {
204 // Inflate the menu; this adds items to the action bar if it is present.
205 getMenuInflater().inflate(R.menu.menu_main, menu);
210 public void onClick(View v) {
211 if (v instanceof ImageButton) {
212 int id = ((ImageButton)v).getId();
213 if (id == R.id.button0) { send("key 0"); }
214 else if (id == R.id.button1) { send("key 1"); }
215 else if (id == R.id.button2) { send("key 2"); }
216 else if (id == R.id.button3) { send("key 3"); }
217 else if (id == R.id.button4) { send("key 4"); }
218 else if (id == R.id.button5) { send("key 5"); }
219 else if (id == R.id.button6) { send("key 6"); }
220 else if (id == R.id.button7) { send("key 7"); }
221 else if (id == R.id.button8) { send("key 8"); }
222 else if (id == R.id.button9) { send("key 9"); }
223 else if (id == R.id.buttonA) { send("key A"); }
224 else if (id == R.id.buttonB) { send("key B"); }
225 else if (id == R.id.buttonC) { send("key C"); }
226 else if (id == R.id.buttonD) { send("key D"); }
227 else if (id == R.id.buttonE) { send("key E"); }
228 else if (id == R.id.buttonF) { send("key F"); }
229 else if (id == R.id.fast_lt) { send("fast_lt"); }
230 else if (id == R.id.media_up) { send("media_up"); }
231 else if (id == R.id.fast_rt) { send("fast_rt"); }
232 else if (id == R.id.menu) { send("menu"); }
233 else if (id == R.id.media_lt) { send("media_lt"); }
234 else if (id == R.id.pause) { send("pause"); }
235 else if (id == R.id.media_rt) { send("media_rt"); }
236 else if (id == R.id.slow_lt) { send("slow_lt"); }
237 else if (id == R.id.media_dn) { send("media_dn"); }
238 else if (id == R.id.slow_rt) { send("slow_rt"); }
239 else if (id == R.id.full_scr) { send("key F"); }
240 else if (id == R.id.stop) { send("stop"); }
241 else if (id == R.id.play) { send("play"); }
244 if (id == R.id.config) {
245 Intent it = new Intent(this, ConfigActivity.class);
246 it.putExtra("IP_ADDR", ip_addr);
247 it.putExtra("PIN", pin);
248 it.putExtra("PORT", dport);
251 else if (id == R.id.suspend)
253 else if (id == R.id.power)
255 else if (id != R.id.exit)
263 public boolean onOptionsItemSelected(MenuItem item) {
264 super.onOptionsItemSelected(item);
265 int id = item.getItemId();
266 if (id == R.id.menu_exit) {
275 public void onPause() {
281 public void onDestroy() {