|
14 | 14 | */ |
15 | 15 | package com.google.example.games.catt2; |
16 | 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 | 17 | import android.app.Activity; |
22 | 18 | import android.content.Context; |
23 | 19 | import android.content.Intent; |
|
32 | 28 | import android.widget.ListView; |
33 | 29 | import android.widget.TextView; |
34 | 30 |
|
| 31 | +import com.google.android.gms.common.images.ImageManager; |
| 32 | +import com.google.android.gms.games.snapshot.Snapshot; |
| 33 | +import com.google.android.gms.games.snapshot.SnapshotMetadata; |
| 34 | + |
35 | 35 | import java.util.ArrayList; |
36 | 36 |
|
37 | 37 | /** |
38 | 38 | * Activity to select a snapshot from a list of snapshots or snapshot metadata. The intended |
39 | 39 | * use of this activity is to present a choice of two conflicting snapshots to the user so |
40 | 40 | * they can select the "best" snapshot. |
41 | | - * |
| 41 | + * <p> |
42 | 42 | * There is also a code path that loads all the saved games and presents this list, which is not |
43 | 43 | * expected to be "production" code, but rather demonstrate how the Snapshots.load() method works. |
44 | 44 | * |
45 | 45 | * @author Clayton Wilkinson (Google) |
46 | 46 | */ |
47 | 47 | public class SelectSnapshotActivity extends Activity implements AdapterView.OnItemClickListener { |
48 | 48 |
|
49 | | - private static final String TAG = "SelSnapshotActivity"; |
50 | | - // intent data which is a snapshot metadata |
51 | | - public static final String SNAPSHOT_METADATA = "snapshotmeta"; |
| 49 | + private static final String TAG = "SelSnapshotActivity"; |
| 50 | + // intent data which is a snapshot metadata |
| 51 | + public static final String SNAPSHOT_METADATA = "snapshotmeta"; |
52 | 52 |
|
53 | | - // intent data that is a list of snapshot metadatas. |
54 | | - public static final String SNAPSHOT_METADATA_LIST = "snapshotmetaList"; |
| 53 | + // intent data that is a list of snapshot metadatas. |
| 54 | + public static final String SNAPSHOT_METADATA_LIST = "snapshotmetaList"; |
55 | 55 |
|
56 | | - // intent data that is the conflict id. used when resolving a conflict. |
57 | | - public static final String CONFLICT_ID = "conflictId"; |
| 56 | + // intent data that is the conflict id. used when resolving a conflict. |
| 57 | + public static final String CONFLICT_ID = "conflictId"; |
58 | 58 |
|
59 | | - // intent data that is the retry count for retrying the conflict resolution. |
60 | | - public static final String RETRY_COUNT = "retrycount"; |
| 59 | + // intent data that is the retry count for retrying the conflict resolution. |
| 60 | + public static final String RETRY_COUNT = "retrycount"; |
61 | 61 |
|
62 | | - // keep these variables for the current activity and return them in the result. |
63 | | - private String mConflictId; |
| 62 | + // keep these variables for the current activity and return them in the result. |
| 63 | + private String mConflictId; |
64 | 64 |
|
65 | | - private int mRetryCount; |
| 65 | + private int mRetryCount; |
66 | 66 |
|
67 | | - @Override |
68 | | - protected void onCreate(Bundle savedInstanceState) { |
69 | | - super.onCreate(savedInstanceState); |
70 | | - setContentView(R.layout.activity_select_snapshot); |
71 | | - |
72 | | - //expect the intent to include the list of snapshots or snapshot metadatas to display |
73 | | - Intent intent = getIntent(); |
74 | | - if (intent != null) { |
75 | | - ArrayList<SnapshotMetadata> snapshotMetadataList; |
76 | | - ListView vw = (ListView) findViewById(R.id.snapshot_list); |
77 | | - |
78 | | - snapshotMetadataList = intent.getParcelableArrayListExtra(SNAPSHOT_METADATA_LIST); |
79 | | - // set a custom list adapter that can display the image and other |
80 | | - // information about a snapshot. |
81 | | - vw.setAdapter( |
82 | | - new SnapshotListAdapter<SnapshotMetadata>(this, snapshotMetadataList)); |
83 | | - |
84 | | - mConflictId = intent.getStringExtra(CONFLICT_ID); |
85 | | - mRetryCount = intent.getIntExtra(RETRY_COUNT, 0); |
86 | | - |
87 | | - // register this class as the listener for when an item is selected |
88 | | - vw.setOnItemClickListener(this); |
89 | | - } |
| 67 | + @Override |
| 68 | + protected void onCreate(Bundle savedInstanceState) { |
| 69 | + super.onCreate(savedInstanceState); |
| 70 | + setContentView(R.layout.activity_select_snapshot); |
| 71 | + |
| 72 | + //expect the intent to include the list of snapshots or snapshot metadatas to display |
| 73 | + Intent intent = getIntent(); |
| 74 | + if (intent != null) { |
| 75 | + ArrayList<SnapshotMetadata> snapshotMetadataList; |
| 76 | + ListView vw = (ListView) findViewById(R.id.snapshot_list); |
| 77 | + |
| 78 | + snapshotMetadataList = intent.getParcelableArrayListExtra(SNAPSHOT_METADATA_LIST); |
| 79 | + // set a custom list adapter that can display the image and other |
| 80 | + // information about a snapshot. |
| 81 | + vw.setAdapter( |
| 82 | + new SnapshotListAdapter<SnapshotMetadata>(this, snapshotMetadataList)); |
| 83 | + |
| 84 | + mConflictId = intent.getStringExtra(CONFLICT_ID); |
| 85 | + mRetryCount = intent.getIntExtra(RETRY_COUNT, 0); |
| 86 | + |
| 87 | + // register this class as the listener for when an item is selected |
| 88 | + vw.setOnItemClickListener(this); |
90 | 89 | } |
| 90 | + } |
91 | 91 |
|
92 | 92 |
|
93 | | - @Override |
94 | | - public void onItemClick(AdapterView<?> adapterView, View view, int position, long listId) { |
| 93 | + @Override |
| 94 | + public void onItemClick(AdapterView<?> adapterView, View view, int position, long listId) { |
| 95 | + |
| 96 | + SnapshotMetadata selected = (SnapshotMetadata) adapterView.getItemAtPosition(position); |
| 97 | + Intent intent = new Intent(Intent.ACTION_DEFAULT); |
95 | 98 |
|
96 | | - SnapshotMetadata selected = (SnapshotMetadata) adapterView.getItemAtPosition(position); |
97 | | - Intent intent = new Intent(Intent.ACTION_DEFAULT); |
| 99 | + intent.putExtra(SNAPSHOT_METADATA, selected.freeze()); |
| 100 | + |
| 101 | + if (mConflictId != null) { |
| 102 | + intent.putExtra(CONFLICT_ID, mConflictId); |
| 103 | + intent.putExtra(RETRY_COUNT, mRetryCount); |
| 104 | + } |
98 | 105 |
|
99 | | - intent.putExtra(SNAPSHOT_METADATA, selected.freeze()); |
| 106 | + Log.d(TAG, "Finishing item at position " + position + " clicked"); |
| 107 | + setResult(RESULT_OK, intent); |
| 108 | + finish(); |
| 109 | + } |
100 | 110 |
|
101 | | - if (mConflictId != null) { |
102 | | - intent.putExtra(CONFLICT_ID, mConflictId); |
103 | | - intent.putExtra(RETRY_COUNT, mRetryCount); |
104 | | - } |
| 111 | + /** |
| 112 | + * Custom mSnapshotMetadataArrayList adapter which holds the snapshot metadata. This is used |
| 113 | + * to |
| 114 | + * display the image and information for each snapshot. |
| 115 | + */ |
| 116 | + static class SnapshotListAdapter<T> extends ArrayAdapter<T> { |
105 | 117 |
|
106 | | - Log.d(TAG, "Finishing item at position " + position + " clicked"); |
107 | | - setResult(RESULT_OK, intent); |
108 | | - finish(); |
| 118 | + |
| 119 | + public SnapshotListAdapter(Activity activity, ArrayList<T> data) { |
| 120 | + super(activity, R.layout.snapshotlayout, data); |
109 | 121 | } |
110 | 122 |
|
| 123 | + @Override |
| 124 | + public View getView(int position, View convertView, ViewGroup parent) { |
| 125 | + |
| 126 | + // load the view used for each item in the mSnapshotMetadataArrayList |
| 127 | + LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( |
| 128 | + Context.LAYOUT_INFLATER_SERVICE); |
| 129 | + View rowView = inflater.inflate(R.layout.snapshotlayout, parent, false); |
| 130 | + |
| 131 | + // get the elements of the view which display the specific data for the snapshot. |
| 132 | + TextView textView = (TextView) rowView.findViewById(R.id.label); |
| 133 | + TextView ageView = (TextView) rowView.findViewById(R.id.age); |
| 134 | + ImageView imageView = (ImageView) rowView.findViewById(R.id.snapshot_icon); |
| 135 | + |
| 136 | + SnapshotMetadata snapshotMetadata; |
| 137 | + T item = getItem(position); |
| 138 | + if (item instanceof Snapshot) { |
| 139 | + snapshotMetadata = ((Snapshot) item).getMetadata(); |
| 140 | + } else { |
| 141 | + snapshotMetadata = (SnapshotMetadata) item; |
| 142 | + } |
| 143 | + textView.setText(snapshotMetadata.getDescription()); |
| 144 | + ageView.setText(getAge(snapshotMetadata.getLastModifiedTimestamp())); |
| 145 | + ImageManager.create(getContext()) |
| 146 | + .loadImage(imageView, snapshotMetadata.getCoverImageUri()); |
| 147 | + |
| 148 | + return rowView; |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | + private static final long MILLIS_PER_MINUTE = 60 * 1000; |
| 154 | + |
| 155 | + private static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE; |
| 156 | + |
| 157 | + private static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR; |
| 158 | + |
111 | 159 | /** |
112 | | - * Custom mSnapshotMetadataArrayList adapter which holds the snapshot metadata. This is used |
113 | | - * to |
114 | | - * display the image and information for each snapshot. |
| 160 | + * Helper function to convert the time difference into a string like "3 days ago" |
| 161 | + * |
| 162 | + * @param timestamp - the time to convert. |
| 163 | + * @return localized string, never null. |
115 | 164 | */ |
116 | | - static class SnapshotListAdapter<T> extends ArrayAdapter<T> { |
117 | | - |
118 | | - |
119 | | - public SnapshotListAdapter(Activity activity, ArrayList<T> data) { |
120 | | - super(activity, R.layout.snapshotlayout, data); |
121 | | - } |
122 | | - |
123 | | - @Override |
124 | | - public View getView(int position, View convertView, ViewGroup parent) { |
125 | | - |
126 | | - // load the view used for each item in the mSnapshotMetadataArrayList |
127 | | - LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( |
128 | | - Context.LAYOUT_INFLATER_SERVICE); |
129 | | - View rowView = inflater.inflate(R.layout.snapshotlayout, parent, false); |
130 | | - |
131 | | - // get the elements of the view which display the specific data for the snapshot. |
132 | | - TextView textView = (TextView) rowView.findViewById(R.id.label); |
133 | | - TextView ageView = (TextView) rowView.findViewById(R.id.age); |
134 | | - ImageView imageView = (ImageView) rowView.findViewById(R.id.snapshot_icon); |
135 | | - |
136 | | - SnapshotMetadata snapshotMetadata; |
137 | | - T item = getItem(position); |
138 | | - if (item instanceof Snapshot) { |
139 | | - snapshotMetadata = ((Snapshot) item).getMetadata(); |
140 | | - } else { |
141 | | - snapshotMetadata = (SnapshotMetadata) item; |
142 | | - } |
143 | | - textView.setText(snapshotMetadata.getDescription()); |
144 | | - ageView.setText(getAge(snapshotMetadata.getLastModifiedTimestamp())); |
145 | | - ImageManager.create(getContext()) |
146 | | - .loadImage(imageView, snapshotMetadata.getCoverImageUri()); |
147 | | - |
148 | | - return rowView; |
149 | | - |
150 | | - } |
151 | | - |
152 | | - |
153 | | - private static final long MILLIS_PER_MINUTE = 60 * 1000; |
154 | | - |
155 | | - private static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE; |
156 | | - |
157 | | - private static final long MILLIS_PER_DAY = 24 * MILLIS_PER_HOUR; |
158 | | - |
159 | | - /** |
160 | | - * Helper function to convert the time difference into a string like "3 days ago" |
161 | | - * |
162 | | - * @param timestamp - the time to convert. |
163 | | - * @return localized string, never null. |
164 | | - */ |
165 | | - private String getAge(long timestamp) { |
166 | | - long delta = System.currentTimeMillis() - timestamp; |
167 | | - int days = (int) (delta / MILLIS_PER_DAY); |
168 | | - |
169 | | - delta = delta % MILLIS_PER_DAY; |
170 | | - int hours = (int) (delta / MILLIS_PER_HOUR); |
171 | | - |
172 | | - delta = delta % MILLIS_PER_HOUR; |
173 | | - int minutes = (int) (delta / MILLIS_PER_MINUTE); |
174 | | - |
175 | | - delta = (int) (delta % MILLIS_PER_MINUTE); |
176 | | - int seconds = (int) delta / 1000; |
177 | | - |
178 | | - if (days > 0) { |
179 | | - return getContext().getString(R.string.format_days_ago, days); |
180 | | - } |
181 | | - if (hours > 0) { |
182 | | - return getContext().getString(R.string.format_hours_ago, hours); |
183 | | - } |
184 | | - if (minutes > 0) { |
185 | | - return getContext().getString(R.string.format_minutes_ago, minutes); |
186 | | - } |
187 | | - if (seconds > 0) { |
188 | | - return getContext().getString(R.string.format_seconds_ago, seconds); |
189 | | - } |
190 | | - return getContext().getString(R.string.moments_ago); |
191 | | - } |
| 165 | + private String getAge(long timestamp) { |
| 166 | + long delta = System.currentTimeMillis() - timestamp; |
| 167 | + int days = (int) (delta / MILLIS_PER_DAY); |
| 168 | + |
| 169 | + delta = delta % MILLIS_PER_DAY; |
| 170 | + int hours = (int) (delta / MILLIS_PER_HOUR); |
| 171 | + |
| 172 | + delta = delta % MILLIS_PER_HOUR; |
| 173 | + int minutes = (int) (delta / MILLIS_PER_MINUTE); |
| 174 | + |
| 175 | + delta = (int) (delta % MILLIS_PER_MINUTE); |
| 176 | + int seconds = (int) delta / 1000; |
| 177 | + |
| 178 | + if (days > 0) { |
| 179 | + return getContext().getString(R.string.format_days_ago, days); |
| 180 | + } |
| 181 | + if (hours > 0) { |
| 182 | + return getContext().getString(R.string.format_hours_ago, hours); |
| 183 | + } |
| 184 | + if (minutes > 0) { |
| 185 | + return getContext().getString(R.string.format_minutes_ago, minutes); |
| 186 | + } |
| 187 | + if (seconds > 0) { |
| 188 | + return getContext().getString(R.string.format_seconds_ago, seconds); |
| 189 | + } |
| 190 | + return getContext().getString(R.string.moments_ago); |
192 | 191 | } |
| 192 | + } |
193 | 193 | } |
0 commit comments