Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / LockController.java @ dd874ae8

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 dd874ae8 Leszek Koltunski
    mLockButton = new TransparentImageButton(act, icon, LinearLayout.LayoutParams.MATCH_PARENT,TransparentImageButton.GRAVITY_MIDDLE);
158 55e6be1d Leszek Koltunski
159
    mLockButton.setOnClickListener( new View.OnClickListener()
160
      {
161
      @Override
162
      public void onClick(View v)
163
        {
164 9523ae28 Leszek Koltunski
        toggleLock(control);
165 55e6be1d Leszek Koltunski
        }
166
      });
167
    }
168
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171 9523ae28 Leszek Koltunski
  public void setState(final Activity act, boolean locked)
172 55e6be1d Leszek Koltunski
    {
173
    act.runOnUiThread(new Runnable()
174
      {
175
      @Override
176
      public void run()
177
        {
178
        if( mLockButton!=null )
179 dd874ae8 Leszek Koltunski
          mLockButton.setIconResource(getLockIcon(locked,false));
180 55e6be1d Leszek Koltunski
        }
181
      });
182
    }
183
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
186 dd874ae8 Leszek Koltunski
  public TransparentImageButton getButton()
187 55e6be1d Leszek Koltunski
    {
188
    return mLockButton;
189
    }
190
  }