Project

General

Profile

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

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

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
import org.distorted.objectlib.main.ObjectPreRender;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

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

    
40
  private ObjectPreRender mPre;
41
  private ImageButton mLockButton;
42
  private long mLockTime;
43
  private Timer mTimer;
44
  private boolean mTimerRunning;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
// PUBLIC API
48

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

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

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

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

    
114
    if( !mTimerRunning )
115
      {
116
      changeIcon(act,true);
117

    
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
                changeIcon(act,false);
134

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

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

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

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

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

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

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

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