Project

General

Profile

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

os-android / src / main / java / org / distorted / os / OSInterface.java @ 5601acdd

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

    
30
  private final WeakReference<Activity> mAct;
31
  private MotionEvent mEvent;
32
  private int mPointer1, mPointer2;
33
  SharedPreferences.Editor mEditor;
34
  SharedPreferences mPreferences;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
  public OSInterface(Activity act)
39
    {
40
    mAct = new WeakReference<>(act);
41
    }
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
// SCREEN
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
  public int getScreenDensity()
48
    {
49
    DisplayMetrics dm = new DisplayMetrics();
50
    mAct.get().getWindowManager().getDefaultDisplay().getMetrics(dm);
51
    return dm.densityDpi;
52
    }
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
// TOUCH CONTROL
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  public void setMotionEvent(MotionEvent event)
59
    {
60
    mEvent = event;
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  public int getAction()
66
    {
67
    switch( mEvent.getActionMasked() )
68
      {
69
      case( MotionEvent.ACTION_DOWN        ): return ACTION_DOWN_1;
70
      case( MotionEvent.ACTION_UP          ): return ACTION_UP_1;
71
      case( MotionEvent.ACTION_POINTER_DOWN): return ACTION_DOWN_2;
72
      case( MotionEvent.ACTION_POINTER_UP  ): return ACTION_UP_2;
73
      case( MotionEvent.ACTION_MOVE        ): return ACTION_MOVE;
74
      }
75

    
76
    return 0;
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  public void upOneOfThem()
82
    {
83
    int index = mEvent.getActionIndex();
84

    
85
         if( index==mEvent.findPointerIndex(mPointer1) ) mPointer1 = INVALID_POINTER_ID;
86
    else if( index==mEvent.findPointerIndex(mPointer2) ) mPointer2 = INVALID_POINTER_ID;
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  public boolean isFirstPressed()
92
    {
93
    return mPointer1!=INVALID_POINTER_ID;
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
  public boolean isSecondPressed()
99
    {
100
    return mPointer2!=INVALID_POINTER_ID;
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  public void pressFirst()
106
    {
107
    mPointer1 = mEvent.getPointerId(0);
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  public void unpressFirst()
113
    {
114
    mPointer1 = INVALID_POINTER_ID;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  public void pressSecond()
120
    {
121
    int index = mEvent.getActionIndex();
122
    mPointer2 = mEvent.getPointerId(index);
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  public void unpressSecond()
128
    {
129
    mPointer2 = INVALID_POINTER_ID;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  public float getFirstX()
135
    {
136
    return mEvent.getX();
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  public float getFirstY()
142
    {
143
    return mEvent.getY();
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  public float getSecondX()
149
    {
150
    int index = mEvent.findPointerIndex(mPointer2);
151
    return mEvent.getX(index);
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  public float getSecondY()
157
    {
158
    int index = mEvent.findPointerIndex(mPointer2);
159
    return mEvent.getY(index);
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
// LOCAL FILES
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  public InputStream openLocalFile(String name)
167
    {
168
    Activity act = mAct.get();
169
    File file = new File(act.getFilesDir(), name);
170
    InputStream stream;
171

    
172
    try
173
      {
174
      stream = new FileInputStream(file);
175
      }
176
    catch(FileNotFoundException fnf)
177
      {
178
      stream = null;
179
      }
180

    
181
    return stream;
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  public InputStream openLocalFile(int id)
187
    {
188
    Resources res = mAct.get().getResources();
189
    return res!=null ? res.openRawResource(id) : null;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
// PREFERENCES
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  public void remove(String key)
197
    {
198
    mEditor.remove(key);
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  public void putInt(String key, int value)
204
    {
205
    mEditor.putInt(key,value);
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  public int getInt(String key, int def)
211
    {
212
    return mPreferences.getInt(key,def);
213
    }
214
}
    (1-1/1)