1 |
55e6be1d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
3f7a4363
|
Leszek Koltunski
|
import java.util.Timer;
|
23 |
|
|
import java.util.TimerTask;
|
24 |
|
|
|
25 |
9523ae28
|
Leszek Koltunski
|
import android.app.Activity;
|
26 |
55e6be1d
|
Leszek Koltunski
|
import android.view.View;
|
27 |
|
|
import android.widget.LinearLayout;
|
28 |
|
|
|
29 |
|
|
import org.distorted.main.R;
|
30 |
|
|
import org.distorted.main.RubikActivity;
|
31 |
9523ae28
|
Leszek Koltunski
|
import org.distorted.objectlib.main.ObjectControl;
|
32 |
55e6be1d
|
Leszek Koltunski
|
|
33 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34 |
|
|
|
35 |
dd1a65c1
|
Leszek Koltunski
|
public class LockController
|
36 |
55e6be1d
|
Leszek Koltunski
|
{
|
37 |
cc88f2fa
|
Leszek Koltunski
|
private static final int LOCK_TIME = 300;
|
38 |
55e6be1d
|
Leszek Koltunski
|
|
39 |
dd874ae8
|
Leszek Koltunski
|
private TransparentImageButton mLockButton;
|
40 |
cc88f2fa
|
Leszek Koltunski
|
private long mLockTime;
|
41 |
|
|
private Timer mTimer;
|
42 |
|
|
private boolean mTimerRunning;
|
43 |
55e6be1d
|
Leszek Koltunski
|
|
44 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
45 |
|
|
// PUBLIC API
|
46 |
|
|
|
47 |
dd1a65c1
|
Leszek Koltunski
|
public LockController()
|
48 |
55e6be1d
|
Leszek Koltunski
|
{
|
49 |
9d591756
|
Leszek Koltunski
|
mTimerRunning= false;
|
50 |
|
|
mLockTime = 0;
|
51 |
55e6be1d
|
Leszek Koltunski
|
}
|
52 |
|
|
|
53 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54 |
|
|
|
55 |
9523ae28
|
Leszek Koltunski
|
public void toggleLock(ObjectControl control)
|
56 |
55e6be1d
|
Leszek Koltunski
|
{
|
57 |
9523ae28
|
Leszek Koltunski
|
control.toggleLock();
|
58 |
|
|
boolean locked = control.retLocked();
|
59 |
dd874ae8
|
Leszek Koltunski
|
mLockButton.setIconResource(getLockIcon(locked,false));
|
60 |
55e6be1d
|
Leszek Koltunski
|
}
|
61 |
|
|
|
62 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63 |
|
|
|
64 |
9523ae28
|
Leszek Koltunski
|
private int getLockIcon(boolean locked, boolean red)
|
65 |
55e6be1d
|
Leszek Koltunski
|
{
|
66 |
9523ae28
|
Leszek Koltunski
|
if( locked )
|
67 |
55e6be1d
|
Leszek Koltunski
|
{
|
68 |
cc88f2fa
|
Leszek Koltunski
|
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 |
55e6be1d
|
Leszek Koltunski
|
}
|
83 |
|
|
else
|
84 |
|
|
{
|
85 |
146661ec
|
Leszek Koltunski
|
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 |
9523ae28
|
Leszek Koltunski
|
private void changeIcon(Activity act, boolean locked, boolean red)
|
95 |
cc88f2fa
|
Leszek Koltunski
|
{
|
96 |
|
|
act.runOnUiThread(new Runnable()
|
97 |
|
|
{
|
98 |
|
|
@Override
|
99 |
|
|
public void run()
|
100 |
|
|
{
|
101 |
|
|
if( mLockButton!=null )
|
102 |
dd874ae8
|
Leszek Koltunski
|
mLockButton.setIconResource(getLockIcon(locked,red));
|
103 |
cc88f2fa
|
Leszek Koltunski
|
}
|
104 |
|
|
});
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
108 |
|
|
|
109 |
9523ae28
|
Leszek Koltunski
|
public void reddenLock(final Activity act, ObjectControl control)
|
110 |
cc88f2fa
|
Leszek Koltunski
|
{
|
111 |
|
|
mLockTime = System.currentTimeMillis();
|
112 |
|
|
|
113 |
|
|
if( !mTimerRunning )
|
114 |
|
|
{
|
115 |
9523ae28
|
Leszek Koltunski
|
boolean locked = control.retLocked();
|
116 |
|
|
changeIcon(act,locked,true);
|
117 |
cc88f2fa
|
Leszek Koltunski
|
|
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 |
9523ae28
|
Leszek Koltunski
|
boolean locked = control.retLocked();
|
134 |
|
|
changeIcon(act,locked,false);
|
135 |
a84c3a25
|
Leszek Koltunski
|
|
136 |
|
|
if( mTimer!=null )
|
137 |
|
|
{
|
138 |
|
|
mTimer.cancel();
|
139 |
|
|
mTimer = null;
|
140 |
|
|
}
|
141 |
|
|
|
142 |
cc88f2fa
|
Leszek Koltunski
|
mTimerRunning = false;
|
143 |
|
|
}
|
144 |
|
|
}
|
145 |
|
|
});
|
146 |
|
|
}
|
147 |
|
|
}, 0, LOCK_TIME);
|
148 |
|
|
}
|
149 |
|
|
}
|
150 |
|
|
|
151 |
146661ec
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152 |
|
|
|
153 |
dd874ae8
|
Leszek Koltunski
|
public void setupButton(final Activity act, ObjectControl control)
|
154 |
55e6be1d
|
Leszek Koltunski
|
{
|
155 |
9523ae28
|
Leszek Koltunski
|
boolean locked = control.retLocked();
|
156 |
|
|
final int icon = getLockIcon(locked,false);
|
157 |
b600ccd9
|
Leszek Koltunski
|
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 |
55e6be1d
|
Leszek Koltunski
|
|
160 |
|
|
mLockButton.setOnClickListener( new View.OnClickListener()
|
161 |
|
|
{
|
162 |
|
|
@Override
|
163 |
|
|
public void onClick(View v)
|
164 |
|
|
{
|
165 |
9523ae28
|
Leszek Koltunski
|
toggleLock(control);
|
166 |
55e6be1d
|
Leszek Koltunski
|
}
|
167 |
|
|
});
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
171 |
|
|
|
172 |
9523ae28
|
Leszek Koltunski
|
public void setState(final Activity act, boolean locked)
|
173 |
55e6be1d
|
Leszek Koltunski
|
{
|
174 |
|
|
act.runOnUiThread(new Runnable()
|
175 |
|
|
{
|
176 |
|
|
@Override
|
177 |
|
|
public void run()
|
178 |
|
|
{
|
179 |
|
|
if( mLockButton!=null )
|
180 |
dd874ae8
|
Leszek Koltunski
|
mLockButton.setIconResource(getLockIcon(locked,false));
|
181 |
55e6be1d
|
Leszek Koltunski
|
}
|
182 |
|
|
});
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
186 |
|
|
|
187 |
dd874ae8
|
Leszek Koltunski
|
public TransparentImageButton getButton()
|
188 |
55e6be1d
|
Leszek Koltunski
|
{
|
189 |
|
|
return mLockButton;
|
190 |
|
|
}
|
191 |
|
|
}
|