Project

General

Profile

« Previous | Next » 

Revision cc5ec229

Added by Leszek Koltunski over 4 years ago

Downloading High Scores: actually downloading scores!

View differences:

src/main/java/org/distorted/magic/RubikScores.java
24 24
import android.os.Bundle;
25 25
import android.support.annotation.NonNull;
26 26
import android.support.v4.app.FragmentActivity;
27
import android.support.v4.view.ViewPager;
28 27
import android.support.v7.app.AlertDialog;
29 28
import android.support.v7.app.AppCompatDialogFragment;
30 29
import android.support.design.widget.TabLayout;
......
37 36

  
38 37
public class RubikScores extends AppCompatDialogFragment
39 38
  {
40
  RubikScoresViewPager mViewPager;
39
  RubikScoresPagerAdapter mPagerAdapter;
40
  RubikScoresViewPager    mViewPager;
41 41

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

  
......
67 67
    final View view = inflater.inflate(R.layout.scores, null);
68 68
    builder.setView(view);
69 69

  
70
    ViewPager viewPager = view.findViewById(R.id.viewpager);
70
    mViewPager = view.findViewById(R.id.viewpager);
71
    mViewPager.startDownload(act);
72

  
71 73
    TabLayout tabLayout = view.findViewById(R.id.sliding_tabs);
72
    mViewPager = new RubikScoresViewPager(act,viewPager);
73
    tabLayout.setupWithViewPager(viewPager);
74
    mPagerAdapter = new RubikScoresPagerAdapter(act,mViewPager);
75
    tabLayout.setupWithViewPager(mViewPager);
76

  
77
    mViewPager.setCurrentItem(0);
74 78

  
75 79
    for (int i = 0; i< RubikSize.LENGTH; i++)
76 80
      {
src/main/java/org/distorted/magic/RubikScoresDownloader.java
19 19

  
20 20
package org.distorted.magic;
21 21

  
22
import android.content.res.Resources;
23

  
22 24
import java.io.InputStream;
23 25
import java.net.HttpURLConnection;
24 26
import java.net.URL;
27
import java.net.UnknownHostException;
25 28

  
26 29
///////////////////////////////////////////////////////////////////////////////////////////////////
27 30

  
......
29 32
  {
30 33
  interface Receiver
31 34
    {
32
    void receive(String[][][][] scores);
35
    void receive(int[][][] country, String[][][] name, String[][][] time);
33 36
    void exception(String exception);
34 37
    }
35 38

  
......
79 82
  private static boolean mRunning = false;
80 83
  private static int mMode = IDLE;
81 84
  private static Receiver mReceiver;
85
  private static Resources mResources;
86
  private static String mPackageName;
82 87

  
83 88
  private static String mScores = "";
84
  private static String[][][][] mValues = new String[RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES][3];
89
  private static int[][][] mCountry = new int   [RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES];
90
  private static String[][][] mName = new String[RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES];
91
  private static String[][][] mTime = new String[RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES];
85 92

  
86 93
///////////////////////////////////////////////////////////////////////////////////////////////////
87 94

  
......
124 131

  
125 132
        if(level>=0 && level<RubikActivity.MAX_SCRAMBLE && place>=0 && place<MAX_PLACES)
126 133
          {
127
          mValues[size][level][place][0] = country;
128
          mValues[size][level][place][1] = name;
129
          mValues[size][level][place][2] = realTime;
134
          int resID = mResources.getIdentifier( country, "drawable", mPackageName);
135
          mCountry[size][level][place] = resID!=0 ? resID:R.drawable.unk;
136
          mName[size][level][place]    = name;
137
          mTime[size][level][place]    = realTime;
130 138
          }
131 139
        }
132 140
      }
......
201 209
          }
202 210
        mScores = sb.toString();
203 211
        }
204
      catch(Exception e)
212
      catch( final Exception e)
205 213
        {
206 214
        mReceiver.exception("biffed it getting HTTPResponse");
207 215
        return false;
208 216
        }
209 217
      }
210
    catch( Exception e )
218
    catch( final UnknownHostException e )
219
      {
220
      mReceiver.exception("No access to Internet");
221
      return false;
222
      }
223
    catch( final SecurityException e )
224
      {
225
      mReceiver.exception("Application not authorized to connect to the Internet");
226
      return false;
227
      }
228
    catch( final Exception e )
211 229
      {
212 230
      mReceiver.exception(e.getMessage());
213 231
      return false;
......
228 246
  static void onPause()
229 247
    {
230 248
    mRunning = false;
231
    mScores = "";
232 249
    }
233 250

  
234 251
///////////////////////////////////////////////////////////////////////////////////////////////////
235 252

  
236
  void download(Receiver receiver)
253
  void download(Receiver receiver, Resources resources, String packageName)
237 254
    {
238 255
    mReceiver = receiver;
256
    mResources= resources;
257
    mPackageName = packageName;
239 258
    mMode = DOWNLOAD;
259

  
240 260
    Thread networkThrd = new Thread(this);
241 261
    networkThrd.start();
242 262
    }
......
258 278
      }
259 279
    catch( Exception e )
260 280
      {
261
      android.util.Log.e("downloader", "Exception downloading records: "+e.getMessage() );
281
      mReceiver.exception("Exception downloading records: "+e.getMessage() );
262 282
      }
263 283

  
264 284
    mRunning = false;
265 285

  
266 286
    if( success )
267 287
      {
268
      mReceiver.receive(mValues);
288
      mReceiver.receive(mCountry, mName, mTime);
269 289
      }
270 290
    }
271 291
}
src/main/java/org/distorted/magic/RubikScoresPagerAdapter.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.magic;
21

  
22
import android.support.annotation.NonNull;
23
import android.support.v4.app.FragmentActivity;
24
import android.support.v4.view.PagerAdapter;
25
import android.view.View;
26
import android.view.ViewGroup;
27

  
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

  
30
class RubikScoresPagerAdapter extends PagerAdapter
31
  {
32
  private FragmentActivity mAct;
33
  private RubikScoresViewPager mPager;
34

  
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

  
37
  RubikScoresPagerAdapter(FragmentActivity act, RubikScoresViewPager viewPager)
38
    {
39
    mAct = act;
40
    mPager = viewPager;
41

  
42
    viewPager.setAdapter(this);
43
    viewPager.setOffscreenPageLimit( RubikSize.LENGTH-1 );
44
    }
45

  
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

  
48
  @Override
49
  @NonNull
50
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
51
    {
52
    RubikScoresView view = new RubikScoresView(mAct);
53
    mPager.setView(position,view);
54
    collection.addView(view);
55

  
56
    return view;
57
    }
58

  
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

  
61
  @Override
62
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
63
    {
64
    collection.removeView((View) view);
65
    }
66

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

  
69
  @Override
70
  public int getCount()
71
    {
72
    return RubikSize.LENGTH;
73
    }
74

  
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

  
77
  @Override
78
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
79
    {
80
    return view == object;
81
    }
82
  }
src/main/java/org/distorted/magic/RubikScoresView.java
20 20
package org.distorted.magic;
21 21

  
22 22
import android.content.Context;
23
import android.content.res.Resources;
24
import android.graphics.BitmapFactory;
23 25
import android.support.v4.app.FragmentActivity;
24 26
import android.util.AttributeSet;
25 27
import android.view.View;
26 28
import android.widget.FrameLayout;
29
import android.widget.ImageView;
27 30
import android.widget.LinearLayout;
28 31
import android.widget.TextView;
29 32

  
......
31 34

  
32 35
public class RubikScoresView extends FrameLayout
33 36
  {
34
  int mPosition;
37
  private boolean mCreated;
35 38

  
36 39
///////////////////////////////////////////////////////////////////////////////////////////////////
37 40

  
38 41
  public RubikScoresView(Context context, AttributeSet attrs, int defStyle)
39 42
    {
40 43
    super(context, attrs, defStyle);
44

  
45
    mCreated = false;
41 46
    }
42 47

  
43 48
///////////////////////////////////////////////////////////////////////////////////////////////////
......
52 57
  public RubikScoresView(Context context)
53 58
    {
54 59
    super(context);
55
    }
56

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

  
59
  public RubikScoresView(Context context, int position)
60
    {
61
    super(context);
62 60

  
63
    mPosition = position;
64 61
    View view = inflate(context, R.layout.scores_downloading, null);
65 62
    addView(view);
66 63
    }
......
75 72

  
76 73
///////////////////////////////////////////////////////////////////////////////////////////////////
77 74

  
78
  void prepareView(FragmentActivity act, final String[][][] values)
75
  void prepareView(FragmentActivity act, final int[][] country, final String[][] name, final String[][] time)
79 76
    {
77
    if( mCreated ) return;
78

  
80 79
    removeAllViews();
81 80

  
82 81
    View tab = inflate(act, R.layout.scores_tab, null);
......
91 90

  
92 91
      for(int j=0; j<RubikScoresDownloader.MAX_PLACES; j++)
93 92
        {
94
        String coun = values[i][j][0];
95
        String name = values[i][j][1];
96
        String time = values[i][j][2];
97

  
98
        if( name!=null && name.length()>0 )
93
        if( name[i][j] != null )
99 94
          {
100 95
          View row = inflate(act, R.layout.level_row, null);
101 96

  
102
          TextView textCoun = row.findViewById(R.id.level_row_country);
97
          ImageView imgCoun = row.findViewById(R.id.level_row_country);
103 98
          TextView textName = row.findViewById(R.id.level_row_name);
104 99
          TextView textTime = row.findViewById(R.id.level_row_time);
105 100

  
106
          textCoun.setText(coun);
107
          textName.setText(name);
108
          textTime.setText(time);
101
          imgCoun.setImageResource(country[i][j]);
102
          textName.setText(name[i][j]);
103
          textTime.setText(time[i][j]);
109 104

  
110 105
          level.addView(row);
111 106
          }
......
113 108

  
114 109
      layout.addView(level);
115 110
      }
111

  
112
    mCreated = true;
116 113
    }
117 114
  }
src/main/java/org/distorted/magic/RubikScoresViewPager.java
19 19

  
20 20
package org.distorted.magic;
21 21

  
22
import android.support.annotation.NonNull;
22
import android.content.Context;
23 23
import android.support.v4.app.FragmentActivity;
24
import android.support.v4.view.PagerAdapter;
25 24
import android.support.v4.view.ViewPager;
26
import android.view.View;
27
import android.view.ViewGroup;
25
import android.util.AttributeSet;
28 26

  
29 27
///////////////////////////////////////////////////////////////////////////////////////////////////
30 28

  
31
class RubikScoresViewPager extends PagerAdapter implements RubikScoresDownloader.Receiver
29
class RubikScoresViewPager extends ViewPager implements RubikScoresDownloader.Receiver
32 30
  {
33
  private FragmentActivity mAct;
31
  FragmentActivity mAct;
34 32
  private RubikScoresView[] mViews;
33
  private int mSelected;
35 34

  
36 35
///////////////////////////////////////////////////////////////////////////////////////////////////
37 36

  
38
  RubikScoresViewPager(FragmentActivity act, ViewPager viewPager)
37
  public RubikScoresViewPager(Context context, AttributeSet attrs)
38
    {
39
    super(context,attrs);
40

  
41
    mViews = new RubikScoresView[RubikSize.LENGTH];
42
    mSelected = 0;
43

  
44
    addOnPageChangeListener(new ViewPager.OnPageChangeListener()
45
      {
46
      @Override
47
      public void onPageSelected(int position)
48
        {
49
        mSelected = position;
50
        }
51

  
52
      @Override
53
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
54
        { }
55

  
56
      @Override
57
      public void onPageScrollStateChanged(int state)
58
        { }
59
      });
60
    }
61

  
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

  
64
  void startDownload(FragmentActivity act)
39 65
    {
40 66
    mAct = act;
41
    mViews = new RubikScoresView[getCount()];
42
    viewPager.setAdapter(this);
43
    viewPager.setOffscreenPageLimit( RubikSize.LENGTH-1 );
67

  
68
    RubikScoresDownloader downloader = new RubikScoresDownloader();
69
    downloader.download(this, mAct.getResources(), mAct.getPackageName());
70
    }
71

  
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

  
74
  void setView(int position, RubikScoresView view)
75
    {
76
    if( position>=0 && position<RubikSize.LENGTH )
77
      {
78
      mViews[position] = view;
79
      }
44 80
    }
45 81

  
46 82
///////////////////////////////////////////////////////////////////////////////////////////////////
47 83

  
48
  public void receive(final String[][][][] scores)
84
  public void receive(final int[][][] country, final String[][][] name, final String[][][] time)
49 85
    {
50 86
    mAct.runOnUiThread(new Runnable()
51 87
      {
52 88
      @Override
53 89
      public void run()
54 90
        {
91
        long time1 = System.currentTimeMillis();
92

  
55 93
        for(int i=0; i<RubikSize.LENGTH; i++)
56 94
          {
57
          mViews[i].prepareView(mAct,scores[i]);
95
          mViews[i].prepareView(mAct, country[i], name[i], time[i]);
58 96
          }
97

  
98
        long time2 = System.currentTimeMillis();
99

  
100
        android.util.Log.e("dd", "time="+(time2-time1));
59 101
        }
60 102
      });
61 103
    }
......
76 118
        }
77 119
      });
78 120
    }
79

  
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

  
82
  @Override
83
  @NonNull
84
  public Object instantiateItem(@NonNull ViewGroup collection, int position)
85
    {
86
    mViews[position] = new RubikScoresView(mAct,position);
87
    collection.addView(mViews[position]);
88

  
89
    boolean allCreated = true;
90

  
91
    for(int i=0; i<RubikSize.LENGTH; i++)
92
      {
93
      if( mViews[i]==null )
94
        {
95
        allCreated = false;
96
        break;
97
        }
98
      }
99

  
100
    if( allCreated )
101
      {
102
      RubikScoresDownloader downloader = new RubikScoresDownloader();
103
      downloader.download(this);
104
      }
105

  
106
    return mViews[position];
107
    }
108

  
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

  
111
  @Override
112
  public void destroyItem(ViewGroup collection, int position, @NonNull Object view)
113
    {
114
    collection.removeView((View) view);
115
    }
116

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

  
119
  @Override
120
  public int getCount()
121
    {
122
    return RubikSize.LENGTH;
123
    }
124

  
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

  
127
  @Override
128
  public boolean isViewFromObject(@NonNull View view, @NonNull Object object)
129
    {
130
    return view == object;
131
    }
132 121
  }
src/main/res/layout/level_row.xml
1 1
<?xml version="1.0" encoding="utf-8"?>
2 2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 3
    android:layout_width="fill_parent"
4
    android:layout_height="32dp"
4
    android:layout_height="50dp"
5
    android:paddingBottom="2dp"
6
    android:paddingTop="2dp"
5 7
    android:orientation="horizontal" >
6 8

  
7
    <TextView
9
    <ImageView
8 10
        android:id="@+id/level_row_country"
9 11
        android:layout_width="match_parent"
10 12
        android:layout_height="match_parent"
src/main/res/layout/scores.xml
12 12
        android:theme="@style/Theme.AppCompat.NoActionBar">
13 13
    </android.support.design.widget.TabLayout>
14 14

  
15
    <android.support.v4.view.ViewPager
15
    <org.distorted.magic.RubikScoresViewPager
16 16
        android:id="@+id/viewpager"
17 17
        android:layout_width="match_parent"
18 18
        android:layout_height="0dp"

Also available in: Unified diff