My favorites | Sign in
Project Home Issues Source
Checkout   Browse   Changes    
 
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
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.radioactiveyak.location_best_practices.UI.fragments;

// TODO Create a richer UI to display places Details. This should include images,
// TODO ratings, reviews, other people checked in here, etc.

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.radioactiveyak.location_best_practices.PlacesConstants;
import com.radioactiveyak.location_best_practices.R;
import com.radioactiveyak.location_best_practices.content_providers.PlaceDetailsContentProvider;
import com.radioactiveyak.location_best_practices.services.PlaceCheckinService;
import com.radioactiveyak.location_best_practices.services.PlaceDetailsUpdateService;

/**
* UI Fragment to display the details for a selected venue.
*/
public class PlaceDetailFragment extends Fragment implements LoaderCallbacks<Cursor> {

/**
* Factory that produces a new {@link PlaceDetailFragment} populated with
* details corresponding to the reference / ID of the venue passed in.
* @param reference Venue Reference
* @param id Venue Unique ID
* @return {@link PlaceDetailFragment}
*/
public static PlaceDetailFragment newInstance(String reference, String id) {
PlaceDetailFragment f = new PlaceDetailFragment();

// Supply reference and ID inputs as arguments.
Bundle args = new Bundle();
args.putString(PlacesConstants.ARGUMENTS_KEY_REFERENCE, reference);
args.putString(PlacesConstants.ARGUMENTS_KEY_ID, id);
f.setArguments(args);

return f;
}

protected static String TAG = "PlaceDetailFragment";

protected String placeReference = null;
protected String placeId = null;

protected Handler handler = new Handler();
protected Activity activity;
protected TextView nameTextView;
protected TextView phoneTextView;
protected TextView addressTextView;
protected TextView ratingTextView;
protected TextView urlTextView;
protected Button checkinButton;
protected TextView checkedInText;

public PlaceDetailFragment() {
super();
}

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
activity = getActivity();

// Query the PlacesDetails Content Provider using a Loader to find
// the details for the selected venue.
if (placeId != null)
getLoaderManager().initLoader(0, null, this);

// Query the Shared Preferences to find the ID of the last venue checked in to.
SharedPreferences sp = activity.getSharedPreferences(PlacesConstants.SHARED_PREFERENCE_FILE, Context.MODE_PRIVATE);
String lastCheckin = sp.getString(PlacesConstants.SP_KEY_LAST_CHECKIN_ID, null);
if (lastCheckin != null )
checkedIn(lastCheckin);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.place_detail, container, false);
nameTextView = (TextView)view.findViewById(R.id.detail_name);
phoneTextView = (TextView)view.findViewById(R.id.detail_phone);
addressTextView = (TextView)view.findViewById(R.id.detail_address);
ratingTextView = (TextView)view.findViewById(R.id.detail_rating);
urlTextView = (TextView)view.findViewById(R.id.detail_url);
checkinButton = (Button)view.findViewById(R.id.checkin_button);
checkedInText = (TextView)view.findViewById(R.id.detail_checkin_text);

checkinButton.setOnClickListener(checkinButtonOnClickListener);

if (getArguments() != null) {
placeReference = getArguments().getString(PlacesConstants.ARGUMENTS_KEY_REFERENCE);
placeId = getArguments().getString(PlacesConstants.ARGUMENTS_KEY_ID);
}
return view;
}

@Override
public void onResume() {
super.onResume();

// Always refresh the details on resume, but don't force
// a refresh to minimize the network usage. Forced updates
// are unnecessary as we force an update when a venue
// is selected in the Place List Activity.
if (placeReference != null && placeId != null)
updatePlace(placeReference, placeId, false);
}

/**
* Start the {@link PlaceDetailsUpdateService} to refresh the details for the
* selected venue.
* @param reference Reference
* @param id Unique Identifier
* @param forceUpdate Force an update
*/
protected void updatePlace(String reference, String id, boolean forceUpdate) {
if (placeReference != null && placeId != null) {
// Start the PlaceDetailsUpdate Service to query the server for details
// on the specified venue. A "forced update" will ignore the caching latency
// rules and query the server.
Intent updateServiceIntent = new Intent(activity, PlaceDetailsUpdateService.class);
updateServiceIntent.putExtra(PlacesConstants.EXTRA_KEY_REFERENCE, reference);
updateServiceIntent.putExtra(PlacesConstants.EXTRA_KEY_ID, id);
updateServiceIntent.putExtra(PlacesConstants.EXTRA_KEY_FORCEREFRESH, forceUpdate);
activity.startService(updateServiceIntent);
}
}

/**
* {@inheritDoc}
* Query the {@link PlaceDetailsContentProvider} for the Phone, Address, Rating, Reference, and Url
* of the selected venue.
* TODO Expand the projection to include any other details you are recording in the Place Detail Content Provider.
*/
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = new String[] {PlaceDetailsContentProvider.KEY_NAME,
PlaceDetailsContentProvider.KEY_PHONE,
PlaceDetailsContentProvider.KEY_ADDRESS,
PlaceDetailsContentProvider.KEY_RATING,
PlaceDetailsContentProvider.KEY_REFERENCE,
PlaceDetailsContentProvider.KEY_URL};

String selection = PlaceDetailsContentProvider.KEY_ID + "='" + placeId + "'";

return new CursorLoader(activity, PlaceDetailsContentProvider.CONTENT_URI,
projection, selection, null, null);
}

/**
* {@inheritDoc}
* When the Loader has completed, schedule an update of the Fragment UI on the main application thread.
*/
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (data.moveToFirst()) {
final String name = data.getString(data.getColumnIndex(PlaceDetailsContentProvider.KEY_NAME));
final String phone = data.getString(data.getColumnIndex(PlaceDetailsContentProvider.KEY_PHONE));
final String address = data.getString(data.getColumnIndex(PlaceDetailsContentProvider.KEY_ADDRESS));
final String rating = data.getString(data.getColumnIndex(PlaceDetailsContentProvider.KEY_RATING));
final String url = data.getString(data.getColumnIndex(PlaceDetailsContentProvider.KEY_URL));

// If we don't have a place reference passed in, we need to look it up and update our details
// accordingly.
if (placeReference == null) {
placeReference = data.getString(data.getColumnIndex(PlaceDetailsContentProvider.KEY_REFERENCE));
updatePlace(placeReference, placeId, true);
}

handler.post(new Runnable () {
public void run() {
nameTextView.setText(name);
phoneTextView.setText(phone);
addressTextView.setText(address);
ratingTextView.setText(rating);
urlTextView.setText(url);
}
});
}
}

/**
* {@inheritDoc}
*/
public void onLoaderReset(Loader<Cursor> loader) {
handler.post(new Runnable () {
public void run() {
nameTextView.setText("");
phoneTextView.setText("");
addressTextView.setText("");
ratingTextView.setText("");
urlTextView.setText("");
}
});
}

/**
* When the Checkin Button is clicked start the {@link PlaceCheckinService} to checkin.
*/
protected OnClickListener checkinButtonOnClickListener = new OnClickListener() {
public void onClick(View view) {
// TODO Pass in additional parameters to your checkin / rating / review service as appropriate
// TODO In some cases you may prefer to open a new Activity with checkin details before initiating the Service.
Intent checkinServiceIntent = new Intent(getActivity(), PlaceCheckinService.class);
checkinServiceIntent.putExtra(PlacesConstants.EXTRA_KEY_REFERENCE, placeReference);
checkinServiceIntent.putExtra(PlacesConstants.EXTRA_KEY_ID, placeId);
checkinServiceIntent.putExtra(PlacesConstants.EXTRA_KEY_TIME_STAMP, System.currentTimeMillis());
getActivity().startService(checkinServiceIntent);
}
};

/**
* Checks to see if the currently displayed venue is the last place checked in to.
* IF it is, it disables the checkin button and update the UI accordingly.
* @param id Checked-in place ID
*/
public void checkedIn(String id) {
if (placeId == null)
Log.e(TAG, "Place ID = null");
boolean checkedIn = id != null && placeId != null && placeId.equals(id);
checkinButton.setEnabled(!checkedIn);
checkedInText.setVisibility(checkedIn ? View.VISIBLE : View.INVISIBLE);
}
}

Change log

r1 by retome...@google.com on Jun 21, 2011   Diff
Initial Commit
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 10196 bytes, 251 lines
Powered by Google Project Hosting