Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikObjectStateActioner.java @ 1cd323dd

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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 android.os.Bundle;
23

    
24
import androidx.annotation.NonNull;
25

    
26
import com.google.android.play.core.review.ReviewInfo;
27
import com.google.android.play.core.review.ReviewManager;
28
import com.google.android.play.core.review.ReviewManagerFactory;
29
import com.google.android.play.core.tasks.OnCompleteListener;
30
import com.google.android.play.core.tasks.OnFailureListener;
31
import com.google.android.play.core.tasks.Task;
32
import com.google.firebase.analytics.FirebaseAnalytics;
33

    
34
import org.distorted.objectlib.helpers.ObjectStateActioner;
35
import org.distorted.objectlib.helpers.TwistyActivity;
36
import org.distorted.objectlib.main.ObjectType;
37

    
38
import org.distorted.dialogs.RubikDialogNewRecord;
39
import org.distorted.dialogs.RubikDialogSolved;
40
import org.distorted.network.RubikScores;
41
import org.distorted.screens.RubikScreenPlay;
42
import org.distorted.screens.RubikScreenSolving;
43
import org.distorted.screens.ScreenList;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
public class RubikObjectStateActioner implements ObjectStateActioner
48
{
49
  private boolean mIsNewRecord;
50
  private long mNewRecord;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  private void analyticsReport(TwistyActivity act, String message, String name, long timeBegin)
55
    {
56
    long elapsed = System.currentTimeMillis() - timeBegin;
57
    String msg = message+" startTime: "+timeBegin+" elapsed: "+elapsed+" name: "+name;
58

    
59
    if( BuildConfig.DEBUG )
60
       {
61
       android.util.Log.d("pre", msg);
62
       }
63
    else
64
      {
65
      RubikActivity ract = (RubikActivity)act;
66
      FirebaseAnalytics analytics = ract.getAnalytics();
67

    
68
      if( analytics!=null )
69
        {
70
        Bundle bundle = new Bundle();
71
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, msg);
72
        analytics.logEvent(FirebaseAnalytics.Event.SHARE, bundle);
73
        }
74
      }
75
    }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  private void reportRecord(TwistyActivity act, String debug, int scrambleNum)
80
    {
81
    RubikActivity ract = (RubikActivity)act;
82
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
83
    RubikScores scores = RubikScores.getInstance();
84

    
85
    int object      = play.getObject();
86
    int level       = play.getLevel();
87
    ObjectType list = ObjectType.getObject(object);
88
    String name     = scores.getName();
89

    
90
    String record = list.name()+" level "+level+" time "+mNewRecord+" isNew: "+mIsNewRecord+" scrambleNum: "+scrambleNum;
91

    
92
    if( BuildConfig.DEBUG )
93
       {
94
       android.util.Log.e("pre", debug);
95
       android.util.Log.e("pre", name);
96
       android.util.Log.e("pre", record);
97
       }
98
    else
99
      {
100
      FirebaseAnalytics analytics = ract.getAnalytics();
101

    
102
      if( analytics!=null )
103
        {
104
        Bundle bundle = new Bundle();
105
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, debug);
106
        bundle.putString(FirebaseAnalytics.Param.CHARACTER, name);
107
        bundle.putString(FirebaseAnalytics.Param.LEVEL, record);
108
        analytics.logEvent(FirebaseAnalytics.Event.LEVEL_UP, bundle);
109
        }
110
      }
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  private void requestReview(TwistyActivity act)
116
    {
117
    final RubikScores scores = RubikScores.getInstance();
118
    int numWins = scores.incrementNumWins();
119

    
120
    if( numWins==7 || numWins==30 || numWins==100 || numWins==200)
121
      {
122
      final long timeBegin = System.currentTimeMillis();
123
      final ReviewManager manager = ReviewManagerFactory.create(act);
124
      Task<ReviewInfo> request = manager.requestReviewFlow();
125

    
126
      request.addOnCompleteListener(new OnCompleteListener<ReviewInfo>()
127
        {
128
        @Override
129
        public void onComplete (@NonNull Task<ReviewInfo> task)
130
          {
131
          if (task.isSuccessful())
132
            {
133
            final String name = scores.getName();
134
            ReviewInfo reviewInfo = task.getResult();
135
            Task<Void> flow = manager.launchReviewFlow(act, reviewInfo);
136

    
137
            flow.addOnFailureListener(new OnFailureListener()
138
              {
139
              @Override
140
              public void onFailure(Exception e)
141
                {
142
                analyticsReport(act,"Failed", name, timeBegin);
143
                }
144
              });
145

    
146
            flow.addOnCompleteListener(new OnCompleteListener<Void>()
147
              {
148
              @Override
149
              public void onComplete(@NonNull Task<Void> task)
150
                {
151
                analyticsReport(act,"Complete", name, timeBegin);
152
                }
153
              });
154
            }
155
          else
156
            {
157
            String name = scores.getName();
158
            analyticsReport(act,"Not Successful", name, timeBegin);
159
            }
160
          }
161
        });
162
      }
163
    }
164

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

    
167
   public void onWinEffectFinished(TwistyActivity act, String debug, int scrambleNum)
168
     {
169
     if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
170
       {
171
       RubikActivity ract = (RubikActivity)act;
172
       Bundle bundle = new Bundle();
173
       bundle.putLong("time", mNewRecord );
174

    
175
       reportRecord(act,debug,scrambleNum);
176
       requestReview(act);
177

    
178
       if( mIsNewRecord )
179
         {
180
         RubikDialogNewRecord dialog = new RubikDialogNewRecord();
181
         dialog.setArguments(bundle);
182
         dialog.show( act.getSupportFragmentManager(), RubikDialogNewRecord.getDialogTag() );
183
         }
184
       else
185
         {
186
         RubikDialogSolved dialog = new RubikDialogSolved();
187
         dialog.setArguments(bundle);
188
         dialog.show( act.getSupportFragmentManager(), RubikDialogSolved.getDialogTag() );
189
         }
190

    
191
       act.runOnUiThread(new Runnable()
192
         {
193
         @Override
194
         public void run()
195
           {
196
           ScreenList.switchScreen( ract, ScreenList.DONE);
197
           }
198
         });
199
       }
200
     }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
   public void onScrambleEffectFinished(TwistyActivity act)
205
     {
206
     RubikActivity ract = (RubikActivity)act;
207

    
208
     RubikScores.getInstance().incrementNumPlays();
209

    
210
     act.runOnUiThread(new Runnable()
211
       {
212
       @Override
213
       public void run()
214
         {
215
         ScreenList.switchScreen( ract, ScreenList.READ);
216
         }
217
       });
218
     }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
   public void onSolved()
223
     {
224
     if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
225
        {
226
        RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
227
        mNewRecord = solving.getRecord();
228

    
229
        if( mNewRecord< 0 )
230
          {
231
          mNewRecord = -mNewRecord;
232
          mIsNewRecord = false;
233
          }
234
        else
235
          {
236
          mIsNewRecord = true;
237
          }
238
        }
239
     }
240
}
(2-2/5)