-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
378 lines (337 loc) · 14.7 KB
/
Copy pathMainActivity.java
File metadata and controls
378 lines (337 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package com.example.marvelselect;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.widget.ViewPager2;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimeZone;
public class MainActivity extends AppCompatActivity {
//API VARIABLES - START
//base URL - can later add more specific paths
/* REPLACE 'xxx' WITH YOUR OWN CHOICE OF ID */
String base = "http://gateway.marvel.com/v1/public/characters?events=xxx&";
String base1 = "http://gateway.marvel.com/v1/public/characters?events=xxx&";
String base2 = "http://gateway.marvel.com/v1/public/characters?events=xxx&";
//declaring MD5 digest hash variable of timestamp, private key, and public key
MessageDigest hash;
//API VARIABLES - END
//VIEWPAGER VARIABLES - START
//array of images - Bitmap values
static ArrayList<Bitmap> images = new ArrayList<>();
static ArrayList<Bitmap> images1 = new ArrayList<>();
static ArrayList<Bitmap> images2 = new ArrayList<>();
//ViewPager2 UI variables
static ViewPager2 head;
static ViewPager2 mid;
static ViewPager2 bottom;
//VIEWPAGER VARIABLES - END
//UI
//TextViews displaying currently selected superhero names
static TextView heroName;
static TextView midName;
static TextView bottomName;
//Button to switch to locker activity
Button create;
//ArrayList - holds character images
static ArrayList<URL> imgList;
//ArrayLists storing names of superheros in each ViewPager2
static ArrayList<String> nameList = new ArrayList<>();
static ArrayList<String> nameList1 = new ArrayList<>();
static ArrayList<String> nameList2 = new ArrayList<>();
//variables containing position of ViewPager - to identify superheros chosen
static int hero1Pos;
static int hero2Pos;
static int hero3Pos;
//position of team in ListView
int listPos = 0;
//constant keys
public static final String EXTRA_HEROPOS1 = "com.example.application.example.EXTRA_HEROPOS1";
public static final String EXTRA_HEROPOS2 = "com.example.application.example.EXTRA_HEROPOS2";
public static final String EXTRA_HEROPOS3 = "com.example.application.example.EXTRA_HEROPOS3";
public static final String EXTRA_LISTPOS = "com.example.application.example.EXTRA_LISTPOS";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//find views
head = findViewById(R.id.id_teaminfo_viewPager_page);
mid = findViewById(R.id.id_main_viewPager_head1);
bottom = findViewById(R.id.id_main_viewPager_head2);
heroName = findViewById(R.id.id_main_name);
midName = findViewById(R.id.id_main_name1);
bottomName = findViewById(R.id.id_main_name2);
create = findViewById(R.id.id_main_button_create);
//OnClickListener for create Button
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//calling method to send user to Locker
openLocker();
//increment variable referencing ListView position
listPos++;
}
});
//ACCESSING API DATA - START
/* INSERT YOUR OWN PUBLIC & PRIVATE API KEY STRINGS HERE */
//creating valid URL to retrieve JSON
//creating timezone for URL to access
TimeZone tz = TimeZone.getTimeZone("EST");
@SuppressLint("SimpleDateFormat") SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
df.setTimeZone(tz);
String ts = df.format(new Date());
//adding timestamp to url
base += "ts=" + ts + "&";
base1 += "ts=" + ts + "&";
base2 += "ts=" + ts + "&";
//creating MD5 digest hash of timestamp, private key, and public key
try {
//looking for MD5 digest hash code
hash = MessageDigest.getInstance("MD5");
//update hash
hash.update((ts + privateKey + publicKey).getBytes(), 0, (ts + privateKey + publicKey).length());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
String hashVal = new BigInteger(1, hash.digest()).toString(16);
//adding public key and hash to url - now the URL can be used!
base += "apikey=" + publicKey + "&hash=" + hashVal;
base1 += "apikey=" + publicKey + "&hash=" + hashVal;
base2 += "apikey=" + publicKey + "&hash=" + hashVal;
//declaring url
URL url = null;
URL url1 = null;
URL url2 = null;
try {
//setting URL with JSON data to url variable
url = new URL(base);
url1 = new URL(base1);
url2 = new URL(base2);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Log.d("URL", url.toString());
//method call
new RetrieveTask(images, 0, nameList).execute(url);
new RetrieveTask(images1, 1, nameList1).execute(url1);
new RetrieveTask(images2, 2, nameList2).execute(url2);
//ACCESSING API DATA - END
}//onCreate
//method to send user to Locker
private void openLocker() {
//intent to Locker class
Intent intent = new Intent(this, Locker.class);
//send data containing ViewPager positions
intent.putExtra(EXTRA_HEROPOS1, hero1Pos);
intent.putExtra(EXTRA_HEROPOS2, hero2Pos);
intent.putExtra(EXTRA_HEROPOS3, hero3Pos);
//send data containing team's position in ListView
intent.putExtra(EXTRA_LISTPOS, listPos);
//startActivity with created intent
startActivity(intent);
}//openLocker
//AsyncTask - running url on separate thread
private static class RetrieveTask extends AsyncTask<URL, Void, ArrayList<URL>> {
//variables
ArrayList<Bitmap> list;
int id;
ArrayList<String> names;
//constructor
public RetrieveTask(ArrayList<Bitmap> list, int id, ArrayList<String> names) {
//set variables
this.list = list;
this.id = id;
this.names = names;
}
@Override
protected ArrayList<URL> doInBackground(URL... urls) {
try {
//create URL object
URL url = urls[0];
//create URLConnection object
URLConnection connection = url.openConnection();
//create InputStream object
InputStream stream = connection.getInputStream();
//create BufferedReader object
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
//concatenate data to one String value
String line;
String data = "";
while((line = reader.readLine()) != null) {
data += line;
}
//close BufferedReader
reader.close();
//initializing imgList
imgList = new ArrayList<>();
//running in for loop in order to access each character from each URL
for(int i = 0; i < 20; i++) {
//converts JSON string to JSONObject
JSONObject dataObj = new JSONObject(data);
//JSON object - data
JSONObject info = dataObj.getJSONObject("data");
//JSON array - results
JSONArray results = info.getJSONArray("results");
//JSON object
JSONObject sup = results.getJSONObject(i);
//JSON object - thumbnail
JSONObject thumb = sup.getJSONObject("thumbnail");
//accessing superhero's name
String name = sup.getString("name");
//separating names of superheros taken from different URLs
if(id == 0)
nameList.add(name);
if(id == 1)
nameList1.add(name);
if(id == 2)
nameList2.add(name);
//accessing superhero's image
String img = thumb.getString("path");
String ext = thumb.getString("extension");
//finalizing URL to make it valid
img += "/landscape_xlarge." + ext;
//create URL variables from img String variable
URL imgUrl = new URL(img);
//adding values to list
imgList.add(imgUrl);
}
//returns ArrayList
return imgList;
} catch (IOException | JSONException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(ArrayList<URL> urls) {
//if ArrayList is not empty
if(urls != null) {
//loops through all superheros in JSON list
for (int i = 0; i < urls.size(); i++) {
//rendering images so that it can be displayed
new LoadImages(list, id).execute(urls.get(i));
}
}
}
}//RetrieveClass
//AsyncTask - loading images from JSON URL
private static class LoadImages extends AsyncTask<URL, Void, Bitmap> {
//variables
ArrayList<Bitmap> list;
int id;
//constructor
public LoadImages(ArrayList<Bitmap> list, int id) {
//set variables
this.list = list;
this.id = id;
}
@Override
protected Bitmap doInBackground(URL... urls) {
URL url = urls[0];
//create URLConnection object
URLConnection connection = null;
try {
connection = url.openConnection();
//create InputStream object
InputStream stream = connection.getInputStream();
//Bitmap object
return BitmapFactory.decodeStream(stream);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Bitmap bitmap) {
//IMPLEMENTING VIEWPAGER - START
//adding bitmap to images ArrayList
list.add(bitmap);
//distinguishing between superheros taken from different URLs
if(id == 0) {
//set head ViewPager to adapter
head.setAdapter(new ViewPagerAdapter(list));
//updates TextView displaying superhero name in order for the data to match up with ViewPager
head.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
//calls displayName function - passing in current position of ViewPager
displayName(position);
super.onPageSelected(position);
}
});
}
else if(id == 1) {
//set mid ViewPager to adapter
mid.setAdapter(new ViewPagerAdapter(list));
//updates TextView displaying superhero name in order for the data to match up with ViewPager
mid.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
//calls displayName function - passing in current position of ViewPager
displayName(position);
super.onPageSelected(position);
}
});
}
else if(id == 2) {
//set bottom ViewPager to adapter
bottom.setAdapter(new ViewPagerAdapter(list));
//updates TextView displaying superhero name in order for the data to match up with ViewPager
bottom.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
//calls displayName function - passing in current position of ViewPager
displayName(position);
super.onPageSelected(position);
}
});
}
//IMPLEMENTING VIEWPAGER - END
}
//method to update superhero name in order for it to match up with ViewPager
private void displayName(int position) {
//distinguishing between superheros taken from different URLs
if(id == 0) {
//set name of superhero shown in ViewPager
heroName.setText(nameList.get(position));
//sets current position of ViewPager
hero1Pos = position;
}
else if(id == 1) {
//set name of superhero shown in ViewPager
midName.setText(nameList1.get(position));
//sets current position of ViewPager
hero2Pos = position;
}
else if(id == 2) {
//set name of superhero shown in ViewPager
bottomName.setText(nameList2.get(position));
//sets current position of ViewPager
hero3Pos = position;
}
}
}//LoadImages
}