Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / LockController.java @ 7bb30586

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 9523ae28 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
21 55e6be1d Leszek Koltunski
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
24 dd1a65c1 Leszek Koltunski
public class LockController
25 55e6be1d Leszek Koltunski
  {
26 cc88f2fa Leszek Koltunski
  private static final int LOCK_TIME = 300;
27 55e6be1d Leszek Koltunski
28 dd874ae8 Leszek Koltunski
  private TransparentImageButton mLockButton;
29 cc88f2fa Leszek Koltunski
  private long mLockTime;
30
  private Timer mTimer;
31
  private boolean mTimerRunning;
32 55e6be1d Leszek Koltunski
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
// PUBLIC API
35
36 dd1a65c1 Leszek Koltunski
  public LockController()
37 55e6be1d Leszek Koltunski
    {
38 9d591756 Leszek Koltunski
    mTimerRunning= false;
39
    mLockTime    = 0;
40 55e6be1d Leszek Koltunski
    }
41
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44 9523ae28 Leszek Koltunski
  public void toggleLock(ObjectControl control)
45 55e6be1d Leszek Koltunski
    {
46 9523ae28 Leszek Koltunski
    control.toggleLock();
47
    boolean locked = control.retLocked();
48 464ade4a leszek
    mLockButton.setImageResource(getLockIcon(locked,false));
49 55e6be1d Leszek Koltunski
    }
50
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 9523ae28 Leszek Koltunski
  private int getLockIcon(boolean locked, boolean red)
54 55e6be1d Leszek Koltunski
    {
55 70688a23 leszek
    return locked ? (red? R.drawable.ui_locked_red : R.drawable.ui_locked) : R.drawable.ui_unlocked;
56 146661ec Leszek Koltunski
    }
57
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 9523ae28 Leszek Koltunski
  private void changeIcon(Activity act, boolean locked, boolean red)
61 cc88f2fa Leszek Koltunski
    {
62
    act.runOnUiThread(new Runnable()
63
      {
64
      @Override
65
      public void run()
66
        {
67
        if( mLockButton!=null )
68 464ade4a leszek
          mLockButton.setImageResource(getLockIcon(locked,red));
69 cc88f2fa Leszek Koltunski
        }
70
      });
71
    }
72
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75 9523ae28 Leszek Koltunski
  public void reddenLock(final Activity act, ObjectControl control)
76 cc88f2fa Leszek Koltunski
    {
77
    mLockTime = System.currentTimeMillis();
78
79
    if( !mTimerRunning )
80
      {
81 9523ae28 Leszek Koltunski
      boolean locked = control.retLocked();
82
      changeIcon(act,locked,true);
83 cc88f2fa Leszek Koltunski
84
      mTimerRunning = true;
85
      mTimer = new Timer();
86
87
      mTimer.scheduleAtFixedRate(new TimerTask()
88
        {
89
        @Override
90
        public void run()
91
          {
92
          act.runOnUiThread(new Runnable()
93
            {
94
            @Override
95
            public void run()
96
              {
97
              if( System.currentTimeMillis()-mLockTime > LOCK_TIME )
98
                {
99 9523ae28 Leszek Koltunski
                boolean locked = control.retLocked();
100
                changeIcon(act,locked,false);
101 a84c3a25 Leszek Koltunski
102
                if( mTimer!=null )
103
                  {
104
                  mTimer.cancel();
105
                  mTimer = null;
106
                  }
107
108 cc88f2fa Leszek Koltunski
                mTimerRunning = false;
109
                }
110
              }
111
            });
112
          }
113
        }, 0, LOCK_TIME);
114
      }
115
    }
116
117 146661ec Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
118
119 dd874ae8 Leszek Koltunski
  public void setupButton(final Activity act, ObjectControl control)
120 55e6be1d Leszek Koltunski
    {
121 9523ae28 Leszek Koltunski
    boolean locked = control.retLocked();
122
    final int icon = getLockIcon(locked,false);
123 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
124 464ade4a leszek
    mLockButton = new TransparentImageButton(act,icon,params);
125 55e6be1d Leszek Koltunski
126
    mLockButton.setOnClickListener( new View.OnClickListener()
127
      {
128
      @Override
129
      public void onClick(View v)
130
        {
131 9523ae28 Leszek Koltunski
        toggleLock(control);
132 55e6be1d Leszek Koltunski
        }
133
      });
134
    }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
138 9523ae28 Leszek Koltunski
  public void setState(final Activity act, boolean locked)
139 55e6be1d Leszek Koltunski
    {
140
    act.runOnUiThread(new Runnable()
141
      {
142
      @Override
143
      public void run()
144
        {
145
        if( mLockButton!=null )
146 464ade4a leszek
          mLockButton.setImageResource(getLockIcon(locked,false));
147 55e6be1d Leszek Koltunski
        }
148
      });
149
    }
150
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153 dd874ae8 Leszek Koltunski
  public TransparentImageButton getButton()
154 55e6be1d Leszek Koltunski
    {
155
    return mLockButton;
156
    }
157
  }