Project

General

Profile

Download (6.54 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / helpers / LockController.java @ 2da68298

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.ImageButton;
28
import android.widget.LinearLayout;
29
30
import org.distorted.main.R;
31
import org.distorted.main.RubikActivity;
32 9523ae28 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
33 55e6be1d Leszek Koltunski
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
36 dd1a65c1 Leszek Koltunski
public class LockController
37 55e6be1d Leszek Koltunski
  {
38 cc88f2fa Leszek Koltunski
  private static final int LOCK_TIME = 300;
39 55e6be1d Leszek Koltunski
40 dd1a65c1 Leszek Koltunski
  private ImageButton mLockButton;
41 cc88f2fa Leszek Koltunski
  private long mLockTime;
42
  private Timer mTimer;
43
  private boolean mTimerRunning;
44 55e6be1d Leszek Koltunski
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
// PUBLIC API
47
48 dd1a65c1 Leszek Koltunski
  public LockController()
49 55e6be1d Leszek Koltunski
    {
50 9d591756 Leszek Koltunski
    mTimerRunning= false;
51
    mLockTime    = 0;
52 55e6be1d Leszek Koltunski
    }
53
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 9523ae28 Leszek Koltunski
  public void toggleLock(ObjectControl control)
57 55e6be1d Leszek Koltunski
    {
58 9523ae28 Leszek Koltunski
    control.toggleLock();
59
    boolean locked = control.retLocked();
60
    mLockButton.setImageResource(getLockIcon(locked,false));
61 55e6be1d Leszek Koltunski
    }
62
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65 9523ae28 Leszek Koltunski
  private int getLockIcon(boolean locked, boolean red)
66 55e6be1d Leszek Koltunski
    {
67 9523ae28 Leszek Koltunski
    if( locked )
68 55e6be1d Leszek Koltunski
      {
69 cc88f2fa Leszek Koltunski
      if( red )
70
        {
71
        return RubikActivity.getDrawable(R.drawable.ui_small_locked_red,
72
                                         R.drawable.ui_medium_locked_red,
73
                                         R.drawable.ui_big_locked_red,
74
                                         R.drawable.ui_huge_locked_red);
75
        }
76
      else
77
        {
78
        return RubikActivity.getDrawable(R.drawable.ui_small_locked,
79
                                         R.drawable.ui_medium_locked,
80
                                         R.drawable.ui_big_locked,
81
                                         R.drawable.ui_huge_locked);
82
        }
83 55e6be1d Leszek Koltunski
      }
84
    else
85
      {
86 146661ec Leszek Koltunski
      return RubikActivity.getDrawable(R.drawable.ui_small_unlocked,
87
                                       R.drawable.ui_medium_unlocked,
88
                                       R.drawable.ui_big_unlocked,
89
                                       R.drawable.ui_huge_unlocked);
90
      }
91
    }
92
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
95 9523ae28 Leszek Koltunski
  private void changeIcon(Activity act, boolean locked, boolean red)
96 cc88f2fa Leszek Koltunski
    {
97
    act.runOnUiThread(new Runnable()
98
      {
99
      @Override
100
      public void run()
101
        {
102
        if( mLockButton!=null )
103 9523ae28 Leszek Koltunski
          mLockButton.setImageResource(getLockIcon(locked,red));
104 cc88f2fa Leszek Koltunski
        }
105
      });
106
    }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
110 9523ae28 Leszek Koltunski
  public void reddenLock(final Activity act, ObjectControl control)
111 cc88f2fa Leszek Koltunski
    {
112
    mLockTime = System.currentTimeMillis();
113
114
    if( !mTimerRunning )
115
      {
116 9523ae28 Leszek Koltunski
      boolean locked = control.retLocked();
117
      changeIcon(act,locked,true);
118 cc88f2fa Leszek Koltunski
119
      mTimerRunning = true;
120
      mTimer = new Timer();
121
122
      mTimer.scheduleAtFixedRate(new TimerTask()
123
        {
124
        @Override
125
        public void run()
126
          {
127
          act.runOnUiThread(new Runnable()
128
            {
129
            @Override
130
            public void run()
131
              {
132
              if( System.currentTimeMillis()-mLockTime > LOCK_TIME )
133
                {
134 9523ae28 Leszek Koltunski
                boolean locked = control.retLocked();
135
                changeIcon(act,locked,false);
136 a84c3a25 Leszek Koltunski
137
                if( mTimer!=null )
138
                  {
139
                  mTimer.cancel();
140
                  mTimer = null;
141
                  }
142
143 cc88f2fa Leszek Koltunski
                mTimerRunning = false;
144
                }
145
              }
146
            });
147
          }
148
        }, 0, LOCK_TIME);
149
      }
150
    }
151
152 146661ec Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
153
154 e019c70b Leszek Koltunski
  public void setupButton(final Activity act, ObjectControl control, final float width)
155 55e6be1d Leszek Koltunski
    {
156 9523ae28 Leszek Koltunski
    boolean locked = control.retLocked();
157
    final int icon = getLockIcon(locked,false);
158 55e6be1d Leszek Koltunski
    mLockButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
159
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 9523ae28 Leszek Koltunski
          mLockButton.setImageResource(getLockIcon(locked,false));
181 55e6be1d Leszek Koltunski
        }
182
      });
183
    }
184
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
187 dd1a65c1 Leszek Koltunski
  public ImageButton getButton()
188 55e6be1d Leszek Koltunski
    {
189
    return mLockButton;
190
    }
191
  }