Project

General

Profile

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

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

1 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 1c327853 Leszek Koltunski
// 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 55e6be1d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.helpers;
11
12 3f7a4363 Leszek Koltunski
import java.util.Timer;
13
import java.util.TimerTask;
14
15 9523ae28 Leszek Koltunski
import android.app.Activity;
16 55e6be1d Leszek Koltunski
import android.view.View;
17
import android.widget.LinearLayout;
18
19
import org.distorted.main.R;
20
import org.distorted.main.RubikActivity;
21 9523ae28 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
22 55e6be1d Leszek Koltunski
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24
25 dd1a65c1 Leszek Koltunski
public class LockController
26 55e6be1d Leszek Koltunski
  {
27 cc88f2fa Leszek Koltunski
  private static final int LOCK_TIME = 300;
28 55e6be1d Leszek Koltunski
29 dd874ae8 Leszek Koltunski
  private TransparentImageButton mLockButton;
30 cc88f2fa Leszek Koltunski
  private long mLockTime;
31
  private Timer mTimer;
32
  private boolean mTimerRunning;
33 55e6be1d Leszek Koltunski
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
// PUBLIC API
36
37 dd1a65c1 Leszek Koltunski
  public LockController()
38 55e6be1d Leszek Koltunski
    {
39 9d591756 Leszek Koltunski
    mTimerRunning= false;
40
    mLockTime    = 0;
41 55e6be1d Leszek Koltunski
    }
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45 9523ae28 Leszek Koltunski
  public void toggleLock(ObjectControl control)
46 55e6be1d Leszek Koltunski
    {
47 9523ae28 Leszek Koltunski
    control.toggleLock();
48
    boolean locked = control.retLocked();
49 dd874ae8 Leszek Koltunski
    mLockButton.setIconResource(getLockIcon(locked,false));
50 55e6be1d Leszek Koltunski
    }
51
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54 9523ae28 Leszek Koltunski
  private int getLockIcon(boolean locked, boolean red)
55 55e6be1d Leszek Koltunski
    {
56 9523ae28 Leszek Koltunski
    if( locked )
57 55e6be1d Leszek Koltunski
      {
58 cc88f2fa Leszek Koltunski
      if( red )
59
        {
60
        return RubikActivity.getDrawable(R.drawable.ui_small_locked_red,
61
                                         R.drawable.ui_medium_locked_red,
62
                                         R.drawable.ui_big_locked_red,
63
                                         R.drawable.ui_huge_locked_red);
64
        }
65
      else
66
        {
67
        return RubikActivity.getDrawable(R.drawable.ui_small_locked,
68
                                         R.drawable.ui_medium_locked,
69
                                         R.drawable.ui_big_locked,
70
                                         R.drawable.ui_huge_locked);
71
        }
72 55e6be1d Leszek Koltunski
      }
73
    else
74
      {
75 146661ec Leszek Koltunski
      return RubikActivity.getDrawable(R.drawable.ui_small_unlocked,
76
                                       R.drawable.ui_medium_unlocked,
77
                                       R.drawable.ui_big_unlocked,
78
                                       R.drawable.ui_huge_unlocked);
79
      }
80
    }
81
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
84 9523ae28 Leszek Koltunski
  private void changeIcon(Activity act, boolean locked, boolean red)
85 cc88f2fa Leszek Koltunski
    {
86
    act.runOnUiThread(new Runnable()
87
      {
88
      @Override
89
      public void run()
90
        {
91
        if( mLockButton!=null )
92 dd874ae8 Leszek Koltunski
          mLockButton.setIconResource(getLockIcon(locked,red));
93 cc88f2fa Leszek Koltunski
        }
94
      });
95
    }
96
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99 9523ae28 Leszek Koltunski
  public void reddenLock(final Activity act, ObjectControl control)
100 cc88f2fa Leszek Koltunski
    {
101
    mLockTime = System.currentTimeMillis();
102
103
    if( !mTimerRunning )
104
      {
105 9523ae28 Leszek Koltunski
      boolean locked = control.retLocked();
106
      changeIcon(act,locked,true);
107 cc88f2fa Leszek Koltunski
108
      mTimerRunning = true;
109
      mTimer = new Timer();
110
111
      mTimer.scheduleAtFixedRate(new TimerTask()
112
        {
113
        @Override
114
        public void run()
115
          {
116
          act.runOnUiThread(new Runnable()
117
            {
118
            @Override
119
            public void run()
120
              {
121
              if( System.currentTimeMillis()-mLockTime > LOCK_TIME )
122
                {
123 9523ae28 Leszek Koltunski
                boolean locked = control.retLocked();
124
                changeIcon(act,locked,false);
125 a84c3a25 Leszek Koltunski
126
                if( mTimer!=null )
127
                  {
128
                  mTimer.cancel();
129
                  mTimer = null;
130
                  }
131
132 cc88f2fa Leszek Koltunski
                mTimerRunning = false;
133
                }
134
              }
135
            });
136
          }
137
        }, 0, LOCK_TIME);
138
      }
139
    }
140
141 146661ec Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143 dd874ae8 Leszek Koltunski
  public void setupButton(final Activity act, ObjectControl control)
144 55e6be1d Leszek Koltunski
    {
145 9523ae28 Leszek Koltunski
    boolean locked = control.retLocked();
146
    final int icon = getLockIcon(locked,false);
147 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
148
    mLockButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
149 55e6be1d Leszek Koltunski
150
    mLockButton.setOnClickListener( new View.OnClickListener()
151
      {
152
      @Override
153
      public void onClick(View v)
154
        {
155 9523ae28 Leszek Koltunski
        toggleLock(control);
156 55e6be1d Leszek Koltunski
        }
157
      });
158
    }
159
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161
162 9523ae28 Leszek Koltunski
  public void setState(final Activity act, boolean locked)
163 55e6be1d Leszek Koltunski
    {
164
    act.runOnUiThread(new Runnable()
165
      {
166
      @Override
167
      public void run()
168
        {
169
        if( mLockButton!=null )
170 dd874ae8 Leszek Koltunski
          mLockButton.setIconResource(getLockIcon(locked,false));
171 55e6be1d Leszek Koltunski
        }
172
      });
173
    }
174
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177 dd874ae8 Leszek Koltunski
  public TransparentImageButton getButton()
178 55e6be1d Leszek Koltunski
    {
179
    return mLockButton;
180
    }
181
  }