Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import java.util.Timer;
23
import java.util.TimerTask;
24

    
25
import android.view.View;
26
import android.widget.ImageButton;
27
import android.widget.LinearLayout;
28

    
29
import org.distorted.main.R;
30
import org.distorted.main.RubikActivity;
31
import org.distorted.objectlib.helpers.TwistyActivity;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public class LockController
36
  {
37
  private static final int LOCK_TIME = 300;
38

    
39
  private ImageButton mLockButton;
40
  private long mLockTime;
41
  private Timer mTimer;
42
  private boolean mTimerRunning;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
// PUBLIC API
46

    
47
  public LockController()
48
    {
49
    mTimerRunning= false;
50
    mLockTime    = 0;
51
    }
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  public void toggleLock(TwistyActivity act)
56
    {
57
    act.toggleLock();
58
    mLockButton.setImageResource(getLockIcon(act,false));
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  private int getLockIcon(TwistyActivity act, boolean red)
64
    {
65
    if( act.retLocked() )
66
      {
67
      if( red )
68
        {
69
        return RubikActivity.getDrawable(R.drawable.ui_small_locked_red,
70
                                         R.drawable.ui_medium_locked_red,
71
                                         R.drawable.ui_big_locked_red,
72
                                         R.drawable.ui_huge_locked_red);
73
        }
74
      else
75
        {
76
        return RubikActivity.getDrawable(R.drawable.ui_small_locked,
77
                                         R.drawable.ui_medium_locked,
78
                                         R.drawable.ui_big_locked,
79
                                         R.drawable.ui_huge_locked);
80
        }
81
      }
82
    else
83
      {
84
      return RubikActivity.getDrawable(R.drawable.ui_small_unlocked,
85
                                       R.drawable.ui_medium_unlocked,
86
                                       R.drawable.ui_big_unlocked,
87
                                       R.drawable.ui_huge_unlocked);
88
      }
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  private void changeIcon(TwistyActivity act, final boolean red)
94
    {
95
    act.runOnUiThread(new Runnable()
96
      {
97
      @Override
98
      public void run()
99
        {
100
        if( mLockButton!=null )
101
          mLockButton.setImageResource(getLockIcon(act,red));
102
        }
103
      });
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  public void reddenLock(final TwistyActivity act)
109
    {
110
    mLockTime = System.currentTimeMillis();
111

    
112
    if( !mTimerRunning )
113
      {
114
      changeIcon(act,true);
115

    
116
      mTimerRunning = true;
117
      mTimer = new Timer();
118

    
119
      mTimer.scheduleAtFixedRate(new TimerTask()
120
        {
121
        @Override
122
        public void run()
123
          {
124
          act.runOnUiThread(new Runnable()
125
            {
126
            @Override
127
            public void run()
128
              {
129
              if( System.currentTimeMillis()-mLockTime > LOCK_TIME )
130
                {
131
                changeIcon(act,false);
132

    
133
                if( mTimer!=null )
134
                  {
135
                  mTimer.cancel();
136
                  mTimer = null;
137
                  }
138

    
139
                mTimerRunning = false;
140
                }
141
              }
142
            });
143
          }
144
        }, 0, LOCK_TIME);
145
      }
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  public void setupButton(final TwistyActivity act, final float width)
151
    {
152
    final int icon = getLockIcon(act,false);
153
    mLockButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
154

    
155
    mLockButton.setOnClickListener( new View.OnClickListener()
156
      {
157
      @Override
158
      public void onClick(View v)
159
        {
160
        toggleLock(act);
161
        }
162
      });
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  public void setState(final TwistyActivity act)
168
    {
169
    act.runOnUiThread(new Runnable()
170
      {
171
      @Override
172
      public void run()
173
        {
174
        if( mLockButton!=null )
175
          mLockButton.setImageResource(getLockIcon(act,false));
176
        }
177
      });
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  public ImageButton getButton()
183
    {
184
    return mLockButton;
185
    }
186
  }
(1-1/4)