1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2023 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.os;
|
11
|
|
12
|
import android.app.Activity;
|
13
|
import android.content.SharedPreferences;
|
14
|
import android.content.res.Resources;
|
15
|
import android.util.DisplayMetrics;
|
16
|
import android.view.MotionEvent;
|
17
|
|
18
|
import java.io.File;
|
19
|
import java.io.FileInputStream;
|
20
|
import java.io.FileNotFoundException;
|
21
|
import java.io.InputStream;
|
22
|
import java.lang.ref.WeakReference;
|
23
|
|
24
|
import org.distorted.objectlib.helpers.ObjectLibInterface;
|
25
|
import org.distorted.objectlib.helpers.OperatingSystemInterface;
|
26
|
|
27
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28
|
|
29
|
public class OSInterface implements OperatingSystemInterface
|
30
|
{
|
31
|
private final WeakReference<Activity> mAct;
|
32
|
private final ObjectLibInterface mOLI;
|
33
|
private final Resources mRes;
|
34
|
|
35
|
private MotionEvent mEvent;
|
36
|
private int mPointer1, mPointer2;
|
37
|
SharedPreferences.Editor mEditor;
|
38
|
SharedPreferences mPreferences;
|
39
|
|
40
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41
|
|
42
|
public OSInterface(Activity act, ObjectLibInterface oli)
|
43
|
{
|
44
|
mAct = new WeakReference<>(act);
|
45
|
mRes = act.getResources();
|
46
|
mOLI = oli;
|
47
|
}
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
// SCREEN
|
51
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52
|
|
53
|
public int getScreenDensity()
|
54
|
{
|
55
|
DisplayMetrics dm = new DisplayMetrics();
|
56
|
mAct.get().getWindowManager().getDefaultDisplay().getMetrics(dm);
|
57
|
return dm.densityDpi;
|
58
|
}
|
59
|
|
60
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61
|
// TOUCH CONTROL
|
62
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63
|
|
64
|
public void setMotionEvent(MotionEvent event)
|
65
|
{
|
66
|
mEvent = event;
|
67
|
}
|
68
|
|
69
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
70
|
|
71
|
public int getAction()
|
72
|
{
|
73
|
switch( mEvent.getActionMasked() )
|
74
|
{
|
75
|
case( MotionEvent.ACTION_DOWN ): return ACTION_DOWN_1;
|
76
|
case( MotionEvent.ACTION_UP ): return ACTION_UP_1;
|
77
|
case( MotionEvent.ACTION_POINTER_DOWN): return ACTION_DOWN_2;
|
78
|
case( MotionEvent.ACTION_POINTER_UP ): return ACTION_UP_2;
|
79
|
case( MotionEvent.ACTION_MOVE ): return ACTION_MOVE;
|
80
|
}
|
81
|
|
82
|
return 0;
|
83
|
}
|
84
|
|
85
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86
|
|
87
|
public void upOneOfThem()
|
88
|
{
|
89
|
int index = mEvent.getActionIndex();
|
90
|
|
91
|
if( index==mEvent.findPointerIndex(mPointer1) ) mPointer1 = INVALID_POINTER_ID;
|
92
|
else if( index==mEvent.findPointerIndex(mPointer2) ) mPointer2 = INVALID_POINTER_ID;
|
93
|
}
|
94
|
|
95
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
96
|
|
97
|
public int getFirstPointerIndex()
|
98
|
{
|
99
|
return mEvent.findPointerIndex(mPointer1);
|
100
|
}
|
101
|
|
102
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
103
|
|
104
|
public int getSecondPointerIndex()
|
105
|
{
|
106
|
return mEvent.findPointerIndex(mPointer2);
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
|
111
|
public boolean isFirstPressed()
|
112
|
{
|
113
|
return mPointer1!=INVALID_POINTER_ID;
|
114
|
}
|
115
|
|
116
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
|
118
|
public boolean isSecondPressed()
|
119
|
{
|
120
|
return mPointer2!=INVALID_POINTER_ID;
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
public void pressFirst()
|
126
|
{
|
127
|
mPointer1 = mEvent.getPointerId(0);
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
public void unpressFirst()
|
133
|
{
|
134
|
mPointer1 = INVALID_POINTER_ID;
|
135
|
}
|
136
|
|
137
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138
|
|
139
|
public void pressSecond()
|
140
|
{
|
141
|
int index = mEvent.getActionIndex();
|
142
|
mPointer2 = mEvent.getPointerId(index);
|
143
|
}
|
144
|
|
145
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
146
|
|
147
|
public void unpressSecond()
|
148
|
{
|
149
|
mPointer2 = INVALID_POINTER_ID;
|
150
|
}
|
151
|
|
152
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
153
|
|
154
|
public float getFirstX()
|
155
|
{
|
156
|
return mEvent.getX();
|
157
|
}
|
158
|
|
159
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
160
|
|
161
|
public float getFirstY()
|
162
|
{
|
163
|
return mEvent.getY();
|
164
|
}
|
165
|
|
166
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
167
|
|
168
|
public float getSecondX()
|
169
|
{
|
170
|
int index = mEvent.findPointerIndex(mPointer2);
|
171
|
return mEvent.getX(index);
|
172
|
}
|
173
|
|
174
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
175
|
|
176
|
public float getSecondY()
|
177
|
{
|
178
|
int index = mEvent.findPointerIndex(mPointer2);
|
179
|
return mEvent.getY(index);
|
180
|
}
|
181
|
|
182
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
183
|
|
184
|
public float getX(int index)
|
185
|
{
|
186
|
return mEvent.getX(index);
|
187
|
}
|
188
|
|
189
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
190
|
|
191
|
public float getY(int index)
|
192
|
{
|
193
|
return mEvent.getY(index);
|
194
|
}
|
195
|
|
196
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
197
|
// LOCAL FILES
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
|
200
|
public InputStream openLocalFile(String name)
|
201
|
{
|
202
|
Activity act = mAct.get();
|
203
|
File file = new File(act.getFilesDir(), name);
|
204
|
InputStream stream;
|
205
|
|
206
|
try
|
207
|
{
|
208
|
stream = new FileInputStream(file);
|
209
|
}
|
210
|
catch(FileNotFoundException fnf)
|
211
|
{
|
212
|
stream = null;
|
213
|
}
|
214
|
|
215
|
return stream;
|
216
|
}
|
217
|
|
218
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
219
|
|
220
|
public InputStream openLocalFile(int id)
|
221
|
{
|
222
|
Resources res = mAct.get().getResources();
|
223
|
return res!=null ? res.openRawResource(id) : null;
|
224
|
}
|
225
|
|
226
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
227
|
// PREFERENCES
|
228
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
229
|
|
230
|
public void setPreferences(SharedPreferences prefs)
|
231
|
{
|
232
|
mPreferences = prefs;
|
233
|
}
|
234
|
|
235
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
236
|
|
237
|
public void setEditor(SharedPreferences.Editor editor)
|
238
|
{
|
239
|
mEditor = editor;
|
240
|
}
|
241
|
|
242
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
243
|
|
244
|
public void remove(String key)
|
245
|
{
|
246
|
mEditor.remove(key);
|
247
|
}
|
248
|
|
249
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
250
|
|
251
|
public void putInt(String key, int value)
|
252
|
{
|
253
|
mEditor.putInt(key,value);
|
254
|
}
|
255
|
|
256
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
257
|
|
258
|
public int getInt(String key, int def)
|
259
|
{
|
260
|
return mPreferences.getInt(key,def);
|
261
|
}
|
262
|
|
263
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
264
|
|
265
|
public void putFloat(String key, float value)
|
266
|
{
|
267
|
mEditor.putFloat(key,value);
|
268
|
}
|
269
|
|
270
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
271
|
|
272
|
public float getFloat(String key, float def)
|
273
|
{
|
274
|
return mPreferences.getFloat(key,def);
|
275
|
}
|
276
|
|
277
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
278
|
|
279
|
public void putString(String key, String value)
|
280
|
{
|
281
|
mEditor.putString(key,value);
|
282
|
}
|
283
|
|
284
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
285
|
|
286
|
public String getString(String key, String def)
|
287
|
{
|
288
|
return mPreferences.getString(key,def);
|
289
|
}
|
290
|
|
291
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
292
|
// STRINGS
|
293
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
294
|
|
295
|
public String getString(int id)
|
296
|
{
|
297
|
return mRes.getString(id);
|
298
|
}
|
299
|
|
300
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
301
|
|
302
|
public String getString(int id, String s1)
|
303
|
{
|
304
|
return mRes.getString(id,s1);
|
305
|
}
|
306
|
|
307
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
308
|
|
309
|
public String getString(int id, String s1, String s2)
|
310
|
{
|
311
|
return mRes.getString(id,s1,s2);
|
312
|
}
|
313
|
|
314
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
315
|
|
316
|
public String getString(int id, String s1, String s2, String s3)
|
317
|
{
|
318
|
return mRes.getString(id,s1,s2,s3);
|
319
|
}
|
320
|
|
321
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
322
|
// ERROR REPORTING
|
323
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
324
|
|
325
|
public void reportError(String error)
|
326
|
{
|
327
|
if( mOLI!=null ) mOLI.reportProblem(error,true);
|
328
|
}
|
329
|
|
330
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
331
|
|
332
|
public ObjectLibInterface getInterface()
|
333
|
{
|
334
|
return mOLI;
|
335
|
}
|
336
|
}
|