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
|
import org.distorted.objectlib.helpers.OperatingSystemInterface;
|
24
|
|
25
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26
|
|
27
|
public class OSInterface implements OperatingSystemInterface
|
28
|
{
|
29
|
private final WeakReference<Activity> mAct;
|
30
|
private final Resources mRes;
|
31
|
|
32
|
private MotionEvent mEvent;
|
33
|
private int mPointer1, mPointer2;
|
34
|
SharedPreferences.Editor mEditor;
|
35
|
SharedPreferences mPreferences;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
|
39
|
public OSInterface(Activity act)
|
40
|
{
|
41
|
mAct = new WeakReference<>(act);
|
42
|
mRes = act.getResources();
|
43
|
}
|
44
|
|
45
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
46
|
// SCREEN
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
|
49
|
public int getScreenDensity()
|
50
|
{
|
51
|
DisplayMetrics dm = new DisplayMetrics();
|
52
|
mAct.get().getWindowManager().getDefaultDisplay().getMetrics(dm);
|
53
|
return dm.densityDpi;
|
54
|
}
|
55
|
|
56
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
57
|
// TOUCH CONTROL
|
58
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
59
|
|
60
|
public void setMotionEvent(MotionEvent event)
|
61
|
{
|
62
|
mEvent = event;
|
63
|
}
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
|
67
|
public int getAction()
|
68
|
{
|
69
|
switch( mEvent.getActionMasked() )
|
70
|
{
|
71
|
case( MotionEvent.ACTION_DOWN ): return ACTION_DOWN_1;
|
72
|
case( MotionEvent.ACTION_UP ): return ACTION_UP_1;
|
73
|
case( MotionEvent.ACTION_POINTER_DOWN): return ACTION_DOWN_2;
|
74
|
case( MotionEvent.ACTION_POINTER_UP ): return ACTION_UP_2;
|
75
|
case( MotionEvent.ACTION_MOVE ): return ACTION_MOVE;
|
76
|
}
|
77
|
|
78
|
return 0;
|
79
|
}
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
|
83
|
public void upOneOfThem()
|
84
|
{
|
85
|
int index = mEvent.getActionIndex();
|
86
|
|
87
|
if( index==mEvent.findPointerIndex(mPointer1) ) mPointer1 = INVALID_POINTER_ID;
|
88
|
else if( index==mEvent.findPointerIndex(mPointer2) ) mPointer2 = INVALID_POINTER_ID;
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
|
93
|
public int getFirstPointerIndex()
|
94
|
{
|
95
|
return mEvent.findPointerIndex(mPointer1);
|
96
|
}
|
97
|
|
98
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99
|
|
100
|
public int getSecondPointerIndex()
|
101
|
{
|
102
|
return mEvent.findPointerIndex(mPointer2);
|
103
|
}
|
104
|
|
105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106
|
|
107
|
public boolean isFirstPressed()
|
108
|
{
|
109
|
return mPointer1!=INVALID_POINTER_ID;
|
110
|
}
|
111
|
|
112
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
113
|
|
114
|
public boolean isSecondPressed()
|
115
|
{
|
116
|
return mPointer2!=INVALID_POINTER_ID;
|
117
|
}
|
118
|
|
119
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120
|
|
121
|
public void pressFirst()
|
122
|
{
|
123
|
mPointer1 = mEvent.getPointerId(0);
|
124
|
}
|
125
|
|
126
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
127
|
|
128
|
public void unpressFirst()
|
129
|
{
|
130
|
mPointer1 = INVALID_POINTER_ID;
|
131
|
}
|
132
|
|
133
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134
|
|
135
|
public void pressSecond()
|
136
|
{
|
137
|
int index = mEvent.getActionIndex();
|
138
|
mPointer2 = mEvent.getPointerId(index);
|
139
|
}
|
140
|
|
141
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142
|
|
143
|
public void unpressSecond()
|
144
|
{
|
145
|
mPointer2 = INVALID_POINTER_ID;
|
146
|
}
|
147
|
|
148
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149
|
|
150
|
public float getFirstX()
|
151
|
{
|
152
|
return mEvent.getX();
|
153
|
}
|
154
|
|
155
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
156
|
|
157
|
public float getFirstY()
|
158
|
{
|
159
|
return mEvent.getY();
|
160
|
}
|
161
|
|
162
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
163
|
|
164
|
public float getSecondX()
|
165
|
{
|
166
|
int index = mEvent.findPointerIndex(mPointer2);
|
167
|
return mEvent.getX(index);
|
168
|
}
|
169
|
|
170
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
171
|
|
172
|
public float getSecondY()
|
173
|
{
|
174
|
int index = mEvent.findPointerIndex(mPointer2);
|
175
|
return mEvent.getY(index);
|
176
|
}
|
177
|
|
178
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179
|
|
180
|
public float getX(int index)
|
181
|
{
|
182
|
return mEvent.getX(index);
|
183
|
}
|
184
|
|
185
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
186
|
|
187
|
public float getY(int index)
|
188
|
{
|
189
|
return mEvent.getY(index);
|
190
|
}
|
191
|
|
192
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
193
|
// LOCAL FILES
|
194
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
195
|
|
196
|
public InputStream openLocalFile(String name)
|
197
|
{
|
198
|
Activity act = mAct.get();
|
199
|
File file = new File(act.getFilesDir(), name);
|
200
|
InputStream stream;
|
201
|
|
202
|
try
|
203
|
{
|
204
|
stream = new FileInputStream(file);
|
205
|
}
|
206
|
catch(FileNotFoundException fnf)
|
207
|
{
|
208
|
stream = null;
|
209
|
}
|
210
|
|
211
|
return stream;
|
212
|
}
|
213
|
|
214
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215
|
|
216
|
public InputStream openLocalFile(int id)
|
217
|
{
|
218
|
Resources res = mAct.get().getResources();
|
219
|
return res!=null ? res.openRawResource(id) : null;
|
220
|
}
|
221
|
|
222
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223
|
// PREFERENCES
|
224
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
225
|
|
226
|
public void setPreferences(SharedPreferences prefs)
|
227
|
{
|
228
|
mPreferences = prefs;
|
229
|
}
|
230
|
|
231
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
232
|
|
233
|
public void setEditor(SharedPreferences.Editor editor)
|
234
|
{
|
235
|
mEditor = editor;
|
236
|
}
|
237
|
|
238
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
239
|
|
240
|
public void remove(String key)
|
241
|
{
|
242
|
mEditor.remove(key);
|
243
|
}
|
244
|
|
245
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
246
|
|
247
|
public void putInt(String key, int value)
|
248
|
{
|
249
|
android.util.Log.e("D", "remembering "+key+" : "+value);
|
250
|
mEditor.putInt(key,value);
|
251
|
}
|
252
|
|
253
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
254
|
|
255
|
public int getInt(String key, int def)
|
256
|
{
|
257
|
int ret = mPreferences.getInt(key,def);
|
258
|
android.util.Log.e("D", "recalling "+key+" : "+ret);
|
259
|
return ret;
|
260
|
}
|
261
|
|
262
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
263
|
// STRINGS
|
264
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
265
|
|
266
|
public String getString(int id)
|
267
|
{
|
268
|
return mRes.getString(id);
|
269
|
}
|
270
|
|
271
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
272
|
|
273
|
public String getString(int id, String s1)
|
274
|
{
|
275
|
return mRes.getString(id,s1);
|
276
|
}
|
277
|
|
278
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
279
|
|
280
|
public String getString(int id, String s1, String s2)
|
281
|
{
|
282
|
return mRes.getString(id,s1,s2);
|
283
|
}
|
284
|
|
285
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
286
|
|
287
|
public String getString(int id, String s1, String s2, String s3)
|
288
|
{
|
289
|
return mRes.getString(id,s1,s2,s3);
|
290
|
}
|
291
|
}
|