Project

General

Profile

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

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

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.app.Activity;
26
import android.view.View;
27
import android.widget.ImageButton;
28
import android.widget.LinearLayout;
29

    
30
import org.distorted.main.R;
31
import org.distorted.main.RubikActivity;
32
import org.distorted.objectlib.main.ObjectControl;
33

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

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

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
// PUBLIC API
47

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

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  public void toggleLock(ObjectControl control)
57
    {
58
    control.toggleLock();
59
    boolean locked = control.retLocked();
60
    mLockButton.setImageResource(getLockIcon(locked,false));
61
    }
62

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

    
65
  private int getLockIcon(boolean locked, boolean red)
66
    {
67
    if( locked )
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(Activity act, boolean locked, boolean red)
96
    {
97
    act.runOnUiThread(new Runnable()
98
      {
99
      @Override
100
      public void run()
101
        {
102
        if( mLockButton!=null )
103
          mLockButton.setImageResource(getLockIcon(locked,red));
104
        }
105
      });
106
    }
107

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

    
110
  public void reddenLock(final Activity act, ObjectControl control)
111
    {
112
    mLockTime = System.currentTimeMillis();
113

    
114
    if( !mTimerRunning )
115
      {
116
      boolean locked = control.retLocked();
117
      changeIcon(act,locked,true);
118

    
119
      mTimerRunning = true;
120
      mTimer = new Timer();
121

    
122
      mTimer.scheduleAtFixedRate(new TimerTask()
123
        {
124
        @Override
125
        public void run()
126
          {
127
          act.runOnUiThread(new Runnable()
128
            {
129
            @Override
130
            public void run()
131
              {
132
              if( System.currentTimeMillis()-mLockTime > LOCK_TIME )
133
                {
134
                boolean locked = control.retLocked();
135
                changeIcon(act,locked,false);
136

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

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

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  public void setupButton(final Activity act, ObjectControl control, final float width)
155
    {
156
    boolean locked = control.retLocked();
157
    final int icon = getLockIcon(locked,false);
158
    mLockButton = new TransparentImageButton(act, icon, width,LinearLayout.LayoutParams.MATCH_PARENT);
159

    
160
    mLockButton.setOnClickListener( new View.OnClickListener()
161
      {
162
      @Override
163
      public void onClick(View v)
164
        {
165
        toggleLock(control);
166
        }
167
      });
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  public void setState(final Activity act, boolean locked)
173
    {
174
    act.runOnUiThread(new Runnable()
175
      {
176
      @Override
177
      public void run()
178
        {
179
        if( mLockButton!=null )
180
          mLockButton.setImageResource(getLockIcon(locked,false));
181
        }
182
      });
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  public ImageButton getButton()
188
    {
189
    return mLockButton;
190
    }
191
  }
(1-1/4)