Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / LockController.java @ 9881dc03

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.objectlib.main.ObjectControl;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

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

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

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
// PUBLIC API
35

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

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  private int getLockIcon(boolean locked, boolean red)
54
    {
55
    return locked ? (red? R.drawable.ui_locked_red : R.drawable.ui_locked) : R.drawable.ui_unlocked;
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  private void changeIcon(Activity act, boolean locked, boolean red)
61
    {
62
    act.runOnUiThread(new Runnable()
63
      {
64
      @Override
65
      public void run()
66
        {
67
        if( mLockButton!=null )
68
          mLockButton.setImageResource(getLockIcon(locked,red));
69
        }
70
      });
71
    }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
  public void reddenLock(final Activity act, ObjectControl control)
76
    {
77
    mLockTime = System.currentTimeMillis();
78

    
79
    if( !mTimerRunning )
80
      {
81
      boolean locked = control.retLocked();
82
      changeIcon(act,locked,true);
83

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

    
102
                if( mTimer!=null )
103
                  {
104
                  mTimer.cancel();
105
                  mTimer = null;
106
                  }
107

    
108
                mTimerRunning = false;
109
                }
110
              }
111
            });
112
          }
113
        }, 0, LOCK_TIME);
114
      }
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  public void setupButton(final Activity act, ObjectControl control)
120
    {
121
    boolean locked = control.retLocked();
122
    final int icon = getLockIcon(locked,false);
123
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
124
    mLockButton = new TransparentImageButton(act,icon,params);
125

    
126
    mLockButton.setOnClickListener( new View.OnClickListener()
127
      {
128
      @Override
129
      public void onClick(View v)
130
        {
131
        toggleLock(control);
132
        }
133
      });
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  public void setState(final Activity act, boolean locked)
139
    {
140
    act.runOnUiThread(new Runnable()
141
      {
142
      @Override
143
      public void run()
144
        {
145
        if( mLockButton!=null )
146
          mLockButton.setImageResource(getLockIcon(locked,false));
147
        }
148
      });
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  public TransparentImageButton getButton()
154
    {
155
    return mLockButton;
156
    }
157
  }
(2-2/6)