Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.helpers;
11

    
12
import java.util.Timer;
13
import java.util.TimerTask;
14

    
15
import android.app.Activity;
16
import android.view.View;
17
import android.widget.LinearLayout;
18

    
19
import org.distorted.main.R;
20
import org.distorted.main.RubikActivity;
21
import org.distorted.objectlib.main.ObjectControl;
22

    
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24

    
25
public class LockController
26
  {
27
  private static final int LOCK_TIME = 300;
28

    
29
  private TransparentImageButton mLockButton;
30
  private long mLockTime;
31
  private Timer mTimer;
32
  private boolean mTimerRunning;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
// PUBLIC API
36

    
37
  public LockController()
38
    {
39
    mTimerRunning= false;
40
    mLockTime    = 0;
41
    }
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
  public void toggleLock(ObjectControl control)
46
    {
47
    control.toggleLock();
48
    boolean locked = control.retLocked();
49
    mLockButton.setIconResource(getLockIcon(locked,false));
50
    }
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  private int getLockIcon(boolean locked, boolean red)
55
    {
56
    if( locked )
57
      {
58
      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
      }
73
    else
74
      {
75
      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
  private void changeIcon(Activity act, boolean locked, boolean red)
85
    {
86
    act.runOnUiThread(new Runnable()
87
      {
88
      @Override
89
      public void run()
90
        {
91
        if( mLockButton!=null )
92
          mLockButton.setIconResource(getLockIcon(locked,red));
93
        }
94
      });
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  public void reddenLock(final Activity act, ObjectControl control)
100
    {
101
    mLockTime = System.currentTimeMillis();
102

    
103
    if( !mTimerRunning )
104
      {
105
      boolean locked = control.retLocked();
106
      changeIcon(act,locked,true);
107

    
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
                boolean locked = control.retLocked();
124
                changeIcon(act,locked,false);
125

    
126
                if( mTimer!=null )
127
                  {
128
                  mTimer.cancel();
129
                  mTimer = null;
130
                  }
131

    
132
                mTimerRunning = false;
133
                }
134
              }
135
            });
136
          }
137
        }, 0, LOCK_TIME);
138
      }
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  public void setupButton(final Activity act, ObjectControl control)
144
    {
145
    boolean locked = control.retLocked();
146
    final int icon = getLockIcon(locked,false);
147
    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

    
150
    mLockButton.setOnClickListener( new View.OnClickListener()
151
      {
152
      @Override
153
      public void onClick(View v)
154
        {
155
        toggleLock(control);
156
        }
157
      });
158
    }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
  public void setState(final Activity act, boolean locked)
163
    {
164
    act.runOnUiThread(new Runnable()
165
      {
166
      @Override
167
      public void run()
168
        {
169
        if( mLockButton!=null )
170
          mLockButton.setIconResource(getLockIcon(locked,false));
171
        }
172
      });
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  public TransparentImageButton getButton()
178
    {
179
    return mLockButton;
180
    }
181
  }
(1-1/4)