-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerExample.java
More file actions
172 lines (127 loc) · 5.64 KB
/
ControllerExample.java
File metadata and controls
172 lines (127 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class ControllerExample extends AppCompatActivity {
//A simple example on how to implement DMD Remote Controller Support
//Our Remotes always send out a broadcast on key press and key release
//You should register the broadcast listener on Resume of your activity and unregister on Pause
//You can further improve the example by adding logic for long press / repeat
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
//Register the Remote Listener
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(remoteListener, new IntentFilter("com.thorkracing.wireddevices.keypress"),
Context.RECEIVER_EXPORTED);
}
else{
registerReceiver(remoteListener, new IntentFilter("com.thorkracing.wireddevices.keypress"));
}
}
@Override
protected void onPause() {
super.onPause();
//Unregister the remote listener
if (remoteListener != null) {
try {
unregisterReceiver(remoteListener);
} catch (Exception ignore) {}
}
}
//Our different Remote Controllers
private enum RemoteDeviceModel {Remote1, Remote2, Remote3, SilverFoxB8J, SilverFoxH1, none}
private RemoteDeviceModel deviceModel = RemoteDeviceModel.none;
private void setRemoteModel(String deviceName) {
switch (deviceName) {
case "DMD-Remote1" -> deviceModel = RemoteDeviceModel.Remote1;
case "DMD-Remote2" -> deviceModel = RemoteDeviceModel.Remote2;
case "DMD-Remote3" -> deviceModel = RemoteDeviceModel.Remote3;
case "SilverFoxB8J" -> deviceModel = RemoteDeviceModel.SilverFoxB8J;
case "SilverFoxH1" -> deviceModel = RemoteDeviceModel.SilverFoxH1;
}
}
private final BroadcastReceiver remoteListener = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null &&
intent.getAction().equals("com.thorkracing.wireddevices.keypress")) {
//When the key is pressed
if (intent.hasExtra("key_press")){
//The Keycode
int keyCode = intent.getIntExtra("key_press",0);
//Detect the device name (in case you want different actions according to device model)
if (intent.hasExtra("deviceName")){
String device = intent.getStringExtra("deviceName");
if (device != null && !device.isEmpty()){
setRemoteModel(device);
}
}
switch (keyCode){
//UP (Usually Map Pan or Menus Select Up)
case 19:
break;
//DOWN (Usually Map Pan or Menus Select Down)
case 20:
break;
//LEFT (Usually Map Pan or Menus Select Left)
case 21:
break;
//RIGHT (Usually Map Pan or Menus Select Right)
case 22:
break;
//ROUND BUTTON 1 (Usually Follow Location Toggle / Center Map)
case 66:
break;
//ROUND BUTTON 2 (Usually Cancel / Back / 3D Mode / Tilt)
case 111:
break;
//SWITCH IN (Usually Zoom In)
case 136:
break;
//SWITCH OUT (Usually Zoom out)
case 137:
break;
}
}
//When the key is released
else if (intent.hasExtra("key_release")){
int keyCode = intent.getIntExtra("key_release",0);
switch (keyCode){
//UP (Usually Map Pan or Menus Select Up)
case 19:
break;
//DOWN (Usually Map Pan or Menus Select Down)
case 20:
break;
//LEFT (Usually Map Pan or Menus Select Left)
case 21:
break;
//RIGHT (Usually Map Pan or Menus Select Right)
case 22:
break;
//ROUND BUTTON 1 (Usually Follow Location Toggle / Center Map)
case 66:
break;
//ROUND BUTTON 2 (Usually Cancel / Back / 3D Mode / Tilt)
case 111:
break;
//SWITCH IN (Usually Zoom In)
case 136:
break;
//SWITCH OUT (Usually Zoom out)
case 137:
break;
}
}
}
}
};
}