|
| 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 | +} |
0 commit comments