Skip to content

Commit 104fc7e

Browse files
committed
CollectAllTheStars2 - Adding a new activity to select the snapshot to use in case of a conflict.
1 parent f591365 commit 104fc7e

7 files changed

Lines changed: 610 additions & 149 deletions

File tree

BasicSamples/CollectAllTheStars2/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
<category android:name="android.intent.category.LAUNCHER" />
5454
</intent-filter>
5555
</activity>
56+
<activity android:name=".SelectSnapshotActivity"
57+
android:label="@string/select_conflicted_game">
58+
</activity>
5659
</application>
5760

5861
</manifest>

BasicSamples/CollectAllTheStars2/src/main/java/com/google/example/games/catt2/MainActivity.java

Lines changed: 334 additions & 149 deletions
Large diffs are not rendered by default.
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/* Copyright (C) 2014 Google Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package com.google.example.games.catt2;
16+
17+
import com.google.android.gms.common.images.ImageManager;
18+
import com.google.android.gms.games.snapshot.Snapshot;
19+
import com.google.android.gms.games.snapshot.SnapshotMetadata;
20+
21+
import android.app.Activity;
22+
import android.content.Context;
23+
import android.content.Intent;
24+
import android.os.Bundle;
25+
import android.view.LayoutInflater;
26+
import android.view.View;
27+
import android.view.ViewGroup;
28+
import android.widget.AdapterView;
29+
import android.widget.ArrayAdapter;
30+
import android.widget.ImageView;
31+
import android.widget.ListView;
32+
import android.widget.TextView;
33+
34+
import java.util.ArrayList;
35+
36+
/**
37+
* Activity to select a snapshot from a list of snapshots or snapshot metadata. The intended
38+
* use of this activity is to present a choice of two conflicting snapshots to the user so
39+
* they can select the "best" snapshot.
40+
*
41+
* There is also a code path that loads all the saved games and presents this list, which is not
42+
* expected to be "production" code, but rather demonstrate how the Snapshots.load() method works.
43+
*
44+
* @author Clayton Wilkinson (Google)
45+
*/
46+
public class SelectSnapshotActivity extends Activity implements AdapterView.OnItemClickListener {
47+
48+
// intent data which is a snapshot.
49+
public static final String SNAPSHOT = "snapshot";
50+
51+
// intent data which is a list of snapshots.
52+
public static final String SNAPSHOT_LIST = "snapshots";
53+
54+
// intent data which is a snapshot metadata
55+
public static final String SNAPSHOT_METADATA = "snapshotmeta";
56+
57+
// intent data that is a list of snapshot metadatas.
58+
public static final String SNAPSHOT_METADATA_LIST = "snapshotmetaList";
59+
60+
// intent data that is the conflict id. used when resolving a conflict.
61+
public static final String CONFLICT_ID = "conflictId";
62+
63+
// intent data that is the retry count for retrying the conflict resolution.
64+
public static final String RETRY_COUNT = "retrycount";
65+
66+
// keep these variables for the current activity and return them in the result.
67+
private String mConflictId;
68+
private int mRetryCount;
69+
70+
@Override
71+
protected void onCreate(Bundle savedInstanceState) {
72+
super.onCreate(savedInstanceState);
73+
setContentView(R.layout.activity_select_snapshot);
74+
75+
//expect the intent to include the list of snapshots or snapshot metadatas to display
76+
Intent intent = getIntent();
77+
if (intent != null) {
78+
ArrayList<SnapshotMetadata> snapshotMetadataList;
79+
ListView vw = (ListView) findViewById(R.id.snapshot_list);
80+
81+
if (intent.hasExtra(SNAPSHOT_LIST)) {
82+
ArrayList<Snapshot> snapshotList =
83+
intent.getParcelableArrayListExtra(SNAPSHOT_LIST);
84+
// set a custom list adapter that can display the image and other
85+
// information about a snapshot.
86+
vw.setAdapter(new SnapshotListAdapter<Snapshot>(this, snapshotList));
87+
}
88+
else if (intent.hasExtra(SNAPSHOT_METADATA_LIST)) {
89+
snapshotMetadataList = intent.getParcelableArrayListExtra(SNAPSHOT_METADATA_LIST);
90+
// set a custom list adapter that can display the image and other
91+
// information about a snapshot.
92+
vw.setAdapter(
93+
new SnapshotListAdapter<SnapshotMetadata>(this, snapshotMetadataList));
94+
}
95+
mConflictId = intent.getStringExtra(CONFLICT_ID);
96+
mRetryCount = intent.getIntExtra(RETRY_COUNT, 0);
97+
98+
// register this class as the listener for when an item is selected
99+
vw.setOnItemClickListener(this);
100+
}
101+
}
102+
103+
104+
@Override
105+
public void onItemClick(AdapterView<?> adapterView, View view, int position, long listId) {
106+
107+
Object selected = adapterView.getItemAtPosition(position);
108+
Intent intent = new Intent(Intent.ACTION_DEFAULT);
109+
110+
// look at the type of the selected item to place it in the correct location in the
111+
// result intent
112+
113+
if (selected instanceof Snapshot) {
114+
intent.putExtra(SNAPSHOT, ((Snapshot) selected).freeze());
115+
} else {
116+
intent.putExtra(SNAPSHOT_METADATA, ((SnapshotMetadata) selected).freeze());
117+
}
118+
if (mConflictId != null) {
119+
intent.putExtra(CONFLICT_ID, mConflictId);
120+
intent.putExtra(RETRY_COUNT, mRetryCount);
121+
}
122+
123+
setResult(RESULT_OK, intent);
124+
finish();
125+
}
126+
127+
/**
128+
* Custom mSnapshotMetadataArrayList adapter which holds the snapshot metadata. This is used
129+
* to
130+
* display the image and information for each snapshot.
131+
*/
132+
static class SnapshotListAdapter<T> extends ArrayAdapter<T> {
133+
134+
135+
public SnapshotListAdapter(Activity activity, ArrayList<T> data) {
136+
super(activity, R.layout.snapshotlayout, data);
137+
}
138+
139+
@Override
140+
public View getView(int position, View convertView, ViewGroup parent) {
141+
142+
// load the view used for each item in the mSnapshotMetadataArrayList
143+
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
144+
Context.LAYOUT_INFLATER_SERVICE);
145+
View rowView = inflater.inflate(R.layout.snapshotlayout, parent, false);
146+
147+
// get the elements of the view which display the specific data for the snapshot.
148+
TextView textView = (TextView) rowView.findViewById(R.id.label);
149+
TextView ageView = (TextView) rowView.findViewById(R.id.age);
150+
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
151+
152+
SnapshotMetadata snapshotMetadata;
153+
T item = getItem(position);
154+
if (item instanceof Snapshot) {
155+
snapshotMetadata = ((Snapshot) item).getMetadata();
156+
} else {
157+
snapshotMetadata = (SnapshotMetadata) item;
158+
}
159+
textView.setText(snapshotMetadata.getDescription());
160+
ageView.setText(getAge(snapshotMetadata.getLastModifiedTimestamp()));
161+
ImageManager.create(getContext())
162+
.loadImage(imageView, snapshotMetadata.getCoverImageUri());
163+
164+
return rowView;
165+
166+
}
167+
168+
169+
private static final long MILLIS_PER_MINUTE = 60 * 1000;
170+
171+
private static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
172+
173+
private static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR;
174+
175+
/**
176+
* Helper function to convert the time difference into a string like "3 days ago"
177+
*
178+
* @param timestamp - the time to convert.
179+
* @return localized string, never null.
180+
*/
181+
private String getAge(long timestamp) {
182+
long delta = System.currentTimeMillis() - timestamp;
183+
int days = (int) (delta / MILLIS_PER_DAY);
184+
185+
delta = delta % MILLIS_PER_DAY;
186+
int hours = (int) (delta / MILLIS_PER_HOUR);
187+
188+
delta = delta % MILLIS_PER_HOUR;
189+
int minutes = (int) (delta / MILLIS_PER_MINUTE);
190+
191+
delta = (int) (delta % MILLIS_PER_MINUTE);
192+
int seconds = (int) delta / 1000;
193+
194+
if (days > 0) {
195+
return getContext().getString(R.string.format_days_ago, days);
196+
}
197+
if (hours > 0) {
198+
return getContext().getString(R.string.format_hours_ago, hours);
199+
}
200+
if (minutes > 0) {
201+
return getContext().getString(R.string.format_minutes_ago, minutes);
202+
}
203+
if (seconds > 0) {
204+
return getContext().getString(R.string.format_seconds_ago, seconds);
205+
}
206+
return getContext().getString(R.string.moments_ago);
207+
}
208+
}
209+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:orientation="vertical"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
style="@style/ConflictDisplay">
8+
<TextView android:text="@string/select_conflicted_game"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:textSize="18sp"
12+
android:textStyle="bold"
13+
android:gravity="center"
14+
android:layout_weight="3"/>
15+
<ListView android:id="@+id/snapshot_list"
16+
android:choiceMode="singleChoice"
17+
android:layout_width="fill_parent" android:layout_height="wrap_content"
18+
android:layout_weight="97"/>
19+
</LinearLayout>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:orientation="vertical"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
<TextView
8+
android:id="@+id/label"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:text="@+id/label"
12+
android:layout_gravity="center"
13+
android:textSize="25sp" >
14+
</TextView>
15+
<ImageView
16+
android:id="@+id/icon"
17+
android:layout_width="fill_parent"
18+
android:layout_height="wrap_content"
19+
>
20+
</ImageView>
21+
<TextView
22+
android:id="@+id/age"
23+
android:layout_width="wrap_content"
24+
android:layout_height="wrap_content"
25+
android:text="@+id/label"
26+
android:layout_gravity="center"
27+
android:textSize="20sp" >
28+
</TextView>
29+
30+
</LinearLayout>

BasicSamples/CollectAllTheStars2/src/main/res/values/strings.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,10 @@
4040
<string name="title_saved_games">"Saved Games"</string>
4141
<string name="title_load_game">Select saved game to load</string>
4242
<string name="please_sign_in">"Please sign-in before playing"</string>
43+
<string name="select_conflicted_game">Select which game to load</string>
44+
<string name="format_days_ago">%d days ago</string>
45+
<string name="format_hours_ago">%d hours ago</string>
46+
<string name="format_minutes_ago">%d minutes ago</string>
47+
<string name="format_seconds_ago">%d seconds ago</string>
48+
<string name="moments_ago">moments ago</string>
4349
</resources>

BasicSamples/CollectAllTheStars2/src/main/res/values/styles.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@
9292
<item name="android:padding">20dp</item>
9393
</style>
9494

95+
<style name="ConflictDisplay">
96+
<item name="android:layout_width">match_parent</item>
97+
<item name="android:layout_height">0dp</item>
98+
<item name="android:layout_weight">1</item>
99+
<item name="android:orientation">vertical</item>
100+
<item name="android:gravity">center</item>
101+
<item name="android:padding">20dp</item>
102+
</style>
103+
95104
<style name="WorldSelectorBar">
96105
<item name="android:layout_width">match_parent</item>
97106
<item name="android:layout_height">wrap_content</item>

0 commit comments

Comments
 (0)