Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikDebug.java @ 09784afd

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

    
22
import org.distorted.scores.RubikScoresDownloader;
23

    
24
import java.util.Timer;
25
import java.util.TimerTask;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
public class RubikDebug
30
  {
31
  private static final int LOOP_INTERVAL = 500;
32
  private static final int LOOP_NUM1     = 5;
33
  private static final int LOOP_NUM2     = 9;
34

    
35
  private String mDebug;
36
  private long mResumeTime;
37
  private int mNumReturned;
38
  private ActivityChanger mChanger;
39

    
40
  public static RubikDebug mThis;
41

    
42
  public interface ActivityChanger
43
    {
44
    void change();
45
    void assign();
46
    }
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
  private void initialize()
51
    {
52
    mDebug      = "";
53
    mResumeTime = 0;
54
    mNumReturned= 0;
55
    }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  private void report(String debug)
60
    {
61
    android.util.Log.e("debug", "Reporting: "+debug);
62

    
63
    RubikScoresDownloader downloader = RubikScoresDownloader.getInstance();
64
    downloader.error(debug);
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  private void check(int loopNum)
70
    {
71
    if( loopNum==LOOP_NUM1 )
72
      {
73
      mChanger.assign();
74
      }
75
    if( loopNum==LOOP_NUM2 )
76
      {
77
      if( mNumReturned!=1 )
78
        {
79
        report(mDebug);
80
        }
81

    
82
      mChanger.change();
83
      }
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  private RubikDebug()
89
    {
90
    initialize();
91
    }
92

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

    
95
  public static RubikDebug getInstance()
96
    {
97
    if( mThis==null ) mThis = new RubikDebug();
98

    
99
    return mThis;
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  public static void addDebug(String debug)
105
    {
106
    if( mThis==null ) mThis = new RubikDebug();
107

    
108
    mThis.mDebug += (debug + "\n");
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  public void onResume(ActivityChanger changer)
114
    {
115
    mChanger = changer;
116
    mResumeTime = System.currentTimeMillis();
117

    
118
    final Timer timer = new Timer();
119

    
120
    timer.scheduleAtFixedRate(new TimerTask()
121
      {
122
      private int mLoopNum = 0;
123

    
124
      @Override
125
      public void run()
126
        {
127
        if( mLoopNum==LOOP_NUM2 )
128
          {
129
          timer.cancel();
130
          }
131

    
132
        check(mLoopNum++);
133
        }
134
      } ,LOOP_INTERVAL, LOOP_INTERVAL);
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  public void onPause()
140
    {
141
    initialize();
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  public void onReturned()
147
    {
148
    mNumReturned++;
149
    }
150
  }
(2-2/5)