1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2021 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.helpers;
|
21
|
|
22
|
import java.util.Timer;
|
23
|
import java.util.TimerTask;
|
24
|
|
25
|
import android.app.Activity;
|
26
|
import android.view.View;
|
27
|
import android.widget.LinearLayout;
|
28
|
|
29
|
import org.distorted.main.R;
|
30
|
import org.distorted.main.RubikActivity;
|
31
|
import org.distorted.objectlib.main.ObjectControl;
|
32
|
|
33
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34
|
|
35
|
public class LockController
|
36
|
{
|
37
|
private static final int LOCK_TIME = 300;
|
38
|
|
39
|
private TransparentImageButton mLockButton;
|
40
|
private long mLockTime;
|
41
|
private Timer mTimer;
|
42
|
private boolean mTimerRunning;
|
43
|
|
44
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
45
|
// PUBLIC API
|
46
|
|
47
|
public LockController()
|
48
|
{
|
49
|
mTimerRunning= false;
|
50
|
mLockTime = 0;
|
51
|
}
|
52
|
|
53
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54
|
|
55
|
public void toggleLock(ObjectControl control)
|
56
|
{
|
57
|
control.toggleLock();
|
58
|
boolean locked = control.retLocked();
|
59
|
mLockButton.setIconResource(getLockIcon(locked,false));
|
60
|
}
|
61
|
|
62
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63
|
|
64
|
private int getLockIcon(boolean locked, boolean red)
|
65
|
{
|
66
|
if( locked )
|
67
|
{
|
68
|
if( red )
|
69
|
{
|
70
|
return RubikActivity.getDrawable(R.drawable.ui_small_locked_red,
|
71
|
R.drawable.ui_medium_locked_red,
|
72
|
R.drawable.ui_big_locked_red,
|
73
|
R.drawable.ui_huge_locked_red);
|
74
|
}
|
75
|
else
|
76
|
{
|
77
|
return RubikActivity.getDrawable(R.drawable.ui_small_locked,
|
78
|
R.drawable.ui_medium_locked,
|
79
|
R.drawable.ui_big_locked,
|
80
|
R.drawable.ui_huge_locked);
|
81
|
}
|
82
|
}
|
83
|
else
|
84
|
{
|
85
|
return RubikActivity.getDrawable(R.drawable.ui_small_unlocked,
|
86
|
R.drawable.ui_medium_unlocked,
|
87
|
R.drawable.ui_big_unlocked,
|
88
|
R.drawable.ui_huge_unlocked);
|
89
|
}
|
90
|
}
|
91
|
|
92
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
93
|
|
94
|
private void changeIcon(Activity act, boolean locked, boolean red)
|
95
|
{
|
96
|
act.runOnUiThread(new Runnable()
|
97
|
{
|
98
|
@Override
|
99
|
public void run()
|
100
|
{
|
101
|
if( mLockButton!=null )
|
102
|
mLockButton.setIconResource(getLockIcon(locked,red));
|
103
|
}
|
104
|
});
|
105
|
}
|
106
|
|
107
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
108
|
|
109
|
public void reddenLock(final Activity act, ObjectControl control)
|
110
|
{
|
111
|
mLockTime = System.currentTimeMillis();
|
112
|
|
113
|
if( !mTimerRunning )
|
114
|
{
|
115
|
boolean locked = control.retLocked();
|
116
|
changeIcon(act,locked,true);
|
117
|
|
118
|
mTimerRunning = true;
|
119
|
mTimer = new Timer();
|
120
|
|
121
|
mTimer.scheduleAtFixedRate(new TimerTask()
|
122
|
{
|
123
|
@Override
|
124
|
public void run()
|
125
|
{
|
126
|
act.runOnUiThread(new Runnable()
|
127
|
{
|
128
|
@Override
|
129
|
public void run()
|
130
|
{
|
131
|
if( System.currentTimeMillis()-mLockTime > LOCK_TIME )
|
132
|
{
|
133
|
boolean locked = control.retLocked();
|
134
|
changeIcon(act,locked,false);
|
135
|
|
136
|
if( mTimer!=null )
|
137
|
{
|
138
|
mTimer.cancel();
|
139
|
mTimer = null;
|
140
|
}
|
141
|
|
142
|
mTimerRunning = false;
|
143
|
}
|
144
|
}
|
145
|
});
|
146
|
}
|
147
|
}, 0, LOCK_TIME);
|
148
|
}
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
|
153
|
public void setupButton(final Activity act, ObjectControl control)
|
154
|
{
|
155
|
boolean locked = control.retLocked();
|
156
|
final int icon = getLockIcon(locked,false);
|
157
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
|
158
|
mLockButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
|
159
|
|
160
|
mLockButton.setOnClickListener( new View.OnClickListener()
|
161
|
{
|
162
|
@Override
|
163
|
public void onClick(View v)
|
164
|
{
|
165
|
toggleLock(control);
|
166
|
}
|
167
|
});
|
168
|
}
|
169
|
|
170
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
171
|
|
172
|
public void setState(final Activity act, boolean locked)
|
173
|
{
|
174
|
act.runOnUiThread(new Runnable()
|
175
|
{
|
176
|
@Override
|
177
|
public void run()
|
178
|
{
|
179
|
if( mLockButton!=null )
|
180
|
mLockButton.setIconResource(getLockIcon(locked,false));
|
181
|
}
|
182
|
});
|
183
|
}
|
184
|
|
185
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
186
|
|
187
|
public TransparentImageButton getButton()
|
188
|
{
|
189
|
return mLockButton;
|
190
|
}
|
191
|
}
|