Project

General

Profile

Download (10.1 KB) Statistics
| Branch: | Revision:

os-android / src / main / java / org / distorted / os / OSInterface.java @ 8a042c76

1 ac3165ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 5601acdd Leszek Koltunski
import android.content.SharedPreferences;
14 e7f14a73 Leszek Koltunski
import android.content.res.Resources;
15 ac3165ed Leszek Koltunski
import android.util.DisplayMetrics;
16 f1721ac5 Leszek Koltunski
import android.view.MotionEvent;
17
18 e7f14a73 Leszek Koltunski
import java.io.File;
19
import java.io.FileInputStream;
20
import java.io.FileNotFoundException;
21
import java.io.InputStream;
22 ac3165ed Leszek Koltunski
import java.lang.ref.WeakReference;
23 f565ccce Leszek Koltunski
24
import org.distorted.objectlib.helpers.ObjectLibInterface;
25 ac3165ed Leszek Koltunski
import org.distorted.objectlib.helpers.OperatingSystemInterface;
26
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
29
public class OSInterface implements OperatingSystemInterface
30
{
31
  private final WeakReference<Activity> mAct;
32 f565ccce Leszek Koltunski
  private final ObjectLibInterface mOLI;
33 dcbdd841 Leszek Koltunski
  private final Resources mRes;
34
35 f1721ac5 Leszek Koltunski
  private MotionEvent mEvent;
36
  private int mPointer1, mPointer2;
37 5601acdd Leszek Koltunski
  SharedPreferences.Editor mEditor;
38
  SharedPreferences mPreferences;
39 ac3165ed Leszek Koltunski
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42 f565ccce Leszek Koltunski
  public OSInterface(Activity act, ObjectLibInterface oli)
43 ac3165ed Leszek Koltunski
    {
44
    mAct = new WeakReference<>(act);
45 dcbdd841 Leszek Koltunski
    mRes = act.getResources();
46 f565ccce Leszek Koltunski
    mOLI = oli;
47 ac3165ed Leszek Koltunski
    }
48
49 f1721ac5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
50 e7f14a73 Leszek Koltunski
// SCREEN
51 ac3165ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53
  public int getScreenDensity()
54
    {
55
    DisplayMetrics dm = new DisplayMetrics();
56
    mAct.get().getWindowManager().getDefaultDisplay().getMetrics(dm);
57
    return dm.densityDpi;
58
    }
59 f1721ac5 Leszek Koltunski
60 e7f14a73 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
61
// TOUCH CONTROL
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64
  public void setMotionEvent(MotionEvent event)
65
    {
66
    mEvent = event;
67
    }
68
69 f1721ac5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71
  public int getAction()
72
    {
73 e3023b62 Leszek Koltunski
    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 f1721ac5 Leszek Koltunski
    }
84
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87 65d2cbf5 Leszek Koltunski
  public void upOneOfThem()
88 f1721ac5 Leszek Koltunski
    {
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 5dcfe278 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 f1721ac5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e7f14a73 Leszek Koltunski
182 5dcfe278 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e7f14a73 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 5601acdd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
227
// PREFERENCES
228 9dac5c41 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 5601acdd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 baae749d Leszek Koltunski
    return mPreferences.getInt(key,def);
261 5601acdd Leszek Koltunski
    }
262 dcbdd841 Leszek Koltunski
263 8a042c76 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
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 dcbdd841 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
292 5dcfe278 Leszek Koltunski
// STRINGS
293 dcbdd841 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 f565ccce Leszek Koltunski
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322
// ERROR REPORTING
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324
325
  public void reportError(String error)
326
    {
327 5009bcd9 Leszek Koltunski
    if( mOLI!=null ) mOLI.reportProblem(error,true);
328
    }
329
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
332
  public ObjectLibInterface getInterface()
333
    {
334
    return mOLI;
335 f565ccce Leszek Koltunski
    }
336 ac3165ed Leszek Koltunski
}