001.
package
com.travel;
002.
003.
import
java.io.BufferedReader;
004.
import
java.io.FileNotFoundException;
005.
import
java.io.IOException;
006.
import
java.io.InputStream;
007.
import
java.io.InputStreamReader;
008.
import
java.net.MalformedURLException;
009.
import
java.util.ArrayList;
010.
import
java.util.List;
011.
012.
import
org.apache.http.HttpEntity;
013.
import
org.apache.http.HttpResponse;
014.
import
org.apache.http.NameValuePair;
015.
import
org.apache.http.StatusLine;
016.
import
org.apache.http.client.ClientProtocolException;
017.
import
org.apache.http.client.HttpClient;
018.
import
org.apache.http.client.entity.UrlEncodedFormEntity;
019.
import
org.apache.http.client.methods.HttpPost;
020.
import
org.apache.http.impl.client.DefaultHttpClient;
021.
import
org.apache.http.message.BasicNameValuePair;
022.
import
org.json.JSONException;
023.
import
org.json.JSONObject;
024.
025.
import
com.travel.AsyncFacebookRunner.RequestListener;
026.
import
com.travel.SessionEvents.AuthListener;
027.
import
com.travel.SessionEvents.LogoutListener;
028.
029.
import
android.os.Bundle;
030.
import
android.os.StrictMode;
031.
import
android.annotation.SuppressLint;
032.
import
android.app.Activity;
033.
import
android.app.AlertDialog;
034.
import
android.content.Intent;
035.
import
android.content.SharedPreferences;
036.
import
android.content.SharedPreferences.Editor;
037.
import
android.support.v4.app.Fragment;
038.
import
android.util.Log;
039.
import
android.view.LayoutInflater;
040.
import
android.view.View;
041.
import
android.view.ViewGroup;
042.
import
android.view.View.OnClickListener;
043.
import
android.widget.Button;
044.
import
android.widget.LinearLayout;
045.
import
android.widget.TextView;
046.
047.
048.
049.
public
class
show_menu
extends
Activity
implements
OnClickListener {
050.
051.
private
static
final
String tag =
"show_menu"
;
052.
private
LoginButton mLoginButton;
053.
String name;
054.
String id;
055.
String idName;
056.
private
Facebook mFacebook;
057.
private
AsyncFacebookRunner mAsyncRunner;
058.
private
TextView txtLogin;
059.
private
TextView txtLoginID;
060.
061.
@SuppressLint
(
"NewApi"
)
062.
@Override
063.
public
void
onCreate(Bundle savedInstanceState) {
064.
super
.onCreate(savedInstanceState);
065.
Log.d(tag, getResources().getString(R.string.CREATING_VIEW));
066.
mFacebook =
new
Facebook(getResources().getString(R.string.FACEBOOK_ID_TEST));
067.
setContentView(R.layout.show_menu);
068.
069.
070.
071.
if
(android.os.Build.VERSION.SDK_INT >
9
) {
072.
StrictMode.ThreadPolicy policy =
new
StrictMode.ThreadPolicy.Builder().permitAll().build();
073.
StrictMode.setThreadPolicy(policy);
074.
}
075.
076.
SharedPreferences pref = getApplicationContext().getSharedPreferences(
"MyPref"
,
0
);
077.
Editor editor = pref.edit();
078.
String status = pref.getString(
"status"
,
""
);
079.
080.
081.
082.
083.
084.
mLoginButton = (LoginButton)
this
.findViewById(R.id.login);
085.
txtLogin = (TextView)
this
.findViewById(R.id.txtShowName);
086.
txtLoginID = (TextView)
this
.findViewById(R.id.txtLoginID);
087.
088.
089.
090.
if
(status ==
"userLogin"
){
091.
txtLogin.setText(status);
092.
093.
editor.putString(
"status"
, status.toString().trim());
094.
editor.commit();
095.
}
096.
else
if
(status ==
"facebookLogin"
){
097.
txtLogin.setText(status);
098.
099.
editor.putString(
"status"
, status.toString().trim());
100.
editor.commit();
101.
mAsyncRunner =
new
AsyncFacebookRunner(mFacebook);
102.
mAsyncRunner.request(
"me"
,
new
SampleRequestListener());
103.
}
104.
105.
106.
107.
108.
109.
110.
SessionStore.restore(mFacebook,
this
);
111.
SessionEvents.addAuthListener(
new
SampleAuthListener());
112.
SessionEvents.addLogoutListener(
new
SampleLogoutListener());
113.
mLoginButton.init(
this
, mFacebook);
114.
115.
116.
Button btnDetailMember = (Button) findViewById(R.id.btnDetailMember);
117.
btnDetailMember.setOnClickListener(
new
OnClickListener() {
118.
@Override
119.
public
void
onClick(View v) {
120.
Intent newActivity =
new
Intent(show_menu.
this
,DetailMember.
class
);
121.
finish();
122.
startActivity(newActivity);
123.
}
124.
});
125.
126.
127.
Button btnLogout = (Button) findViewById(R.id.btnLogout);
128.
btnLogout.setOnClickListener(
new
OnClickListener() {
129.
@Override
130.
public
void
onClick(View v) {
131.
SharedPreferences pref = getApplicationContext().getSharedPreferences(
"MyPref"
,
0
);
132.
Editor editor = pref.edit();
133.
String status = pref.getString(
"status"
,
""
);
134.
135.
if
(status ==
"userLogin"
){
136.
editor.clear();
137.
editor.commit();
138.
Intent newActivity =
new
Intent(show_menu.
this
,Main.
class
);
139.
finish();
140.
startActivity(newActivity);
141.
}
142.
else
{
143.
mLoginButton.performClick();
144.
}
145.
146.
}
147.
});
148.
149.
150.
Button btnAdmin = (Button) findViewById(R.id.btnAdmin);
151.
btnAdmin.setOnClickListener(
new
OnClickListener() {
152.
@Override
153.
public
void
onClick(View v) {
154.
Intent newActivity =
new
Intent(show_menu.
this
,Main_tab.
class
);
155.
finish();
156.
startActivity(newActivity);
157.
}
158.
});
159.
160.
161.
}
162.
@Override
163.
public
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data)
164.
{
165.
super
.onActivityResult(requestCode, resultCode, data);
166.
mFacebook.authorizeCallback(requestCode, resultCode, data);
167.
}
168.
169.
170.
public
class
SampleAuthListener
implements
AuthListener
171.
{
172.
173.
@Override
174.
public
void
onAuthSucceed()
175.
{
176.
177.
178.
}
179.
180.
@Override
181.
public
void
onAuthFail(String error)
182.
{
183.
184.
}
185.
}
186.
187.
public
class
SampleLogoutListener
implements
LogoutListener
188.
{
189.
@Override
190.
public
void
onLogoutBegin()
191.
{
192.
193.
}
194.
195.
@Override
196.
public
void
onLogoutFinish()
197.
{
198.
Intent intent =
new
Intent(show_menu.
this
,Main.
class
);
199.
finish();
200.
startActivity(intent);
201.
}
202.
}
203.
204.
public
class
SampleRequestListener
extends
BaseRequestListener
205.
{
206.
207.
@Override
208.
public
void
onComplete(
final
String response,
final
Object state)
209.
{
210.
try
211.
{
212.
213.
Log.d(
"Facebook-Example"
,
"Response: "
+ response.toString());
214.
JSONObject json = Util.parseJson(response);
215.
name = json.getString(
"name"
);
216.
id = json.getString(
"id"
);
217.
218.
219.
220.
221.
222.
show_menu.
this
.runOnUiThread(
new
Runnable()
223.
{
224.
@Override
225.
public
void
run()
226.
{
227.
228.
txtLoginID.setText(id);
229.
}
230.
});
231.
}
232.
catch
(JSONException e)
233.
{
234.
Log.w(
"Facebook-Example"
,
"JSON Error in response"
);
235.
}
236.
catch
(FacebookError e)
237.
{
238.
Log.w(
"Facebook-Example"
,
"Facebook Error: "
+ e.getMessage());
239.
}
240.
}
241.
}
242.
243.
public
class
SampleUploadListener
extends
BaseRequestListener
244.
{
245.
246.
@Override
247.
public
void
onComplete(
final
String response,
final
Object state)
248.
{
249.
try
250.
{
251.
252.
Log.d(
"Facebook-Example"
,
"Response: "
+ response.toString());
253.
JSONObject json = Util.parseJson(response);
254.
final
String src = json.getString(
"src"
);
255.
256.
257.
258.
259.
260.
show_menu.
this
.runOnUiThread(
new
Runnable()
261.
{
262.
@Override
263.
public
void
run()
264.
{
265.
266.
}
267.
});
268.
}
269.
catch
(JSONException e)
270.
{
271.
Log.w(
"Facebook-Example"
,
"JSON Error in response"
);
272.
}
273.
catch
(FacebookError e)
274.
{
275.
Log.w(
"Facebook-Example"
,
"Facebook Error: "
+ e.getMessage());
276.
}
277.
}
278.
}
279.
280.
public
class
WallPostRequestListener
extends
BaseRequestListener
281.
{
282.
283.
@Override
284.
public
void
onComplete(
final
String response,
final
Object state)
285.
{
286.
Log.d(
"Facebook-Example"
,
"Got response: "
+ response);
287.
String message =
"<empty>"
;
288.
try
289.
{
290.
JSONObject json = Util.parseJson(response);
291.
message = json.getString(
"message"
);
292.
}
293.
catch
(JSONException e)
294.
{
295.
Log.w(
"Facebook-Example"
,
"JSON Error in response"
);
296.
}
297.
catch
(FacebookError e)
298.
{
299.
Log.w(
"Facebook-Example"
,
"Facebook Error: "
+ e.getMessage());
300.
}
301.
final
String text =
"Your Wall Post: "
+ message;
302.
show_menu.
this
.runOnUiThread(
new
Runnable()
303.
{
304.
@Override
305.
public
void
run()
306.
{
307.
308.
}
309.
});
310.
}
311.
}
312.
313.
public
class
WallPostDeleteListener
extends
BaseRequestListener
314.
{
315.
316.
@Override
317.
public
void
onComplete(
final
String response,
final
Object state)
318.
{
319.
if
(response.equals(
"true"
))
320.
{
321.
Log.d(
"Facebook-Example"
,
"Successfully deleted wall post"
);
322.
show_menu.
this
.runOnUiThread(
new
Runnable()
323.
{
324.
@Override
325.
public
void
run()
326.
{
327.
328.
}
329.
});
330.
}
331.
else
332.
{
333.
Log.d(
"Facebook-Example"
,
"Could not delete wall post"
);
334.
}
335.
}
336.
}
337.
338.
public
class
SampleDialogListener
extends
BaseDialogListener
339.
{
340.
341.
@Override
342.
public
void
onComplete(Bundle values)
343.
{
344.
final
String postId = values.getString(
"post_id"
);
345.
if
(postId !=
null
)
346.
{
347.
Log.d(
"Facebook-Example"
,
"Dialog Success! post_id="
+ postId);
348.
mAsyncRunner.request(postId,
new
WallPostRequestListener());
349.
350.
}
351.
else
352.
{
353.
Log.d(
"Facebook-Example"
,
"No wall post made"
);
354.
}
355.
}
356.
}
357.
358.
359.
360.
361.
362.
363.
public
void
onClick(View v)
364.
{
365.
366.
if
(v == mLoginButton)
367.
{
368.
369.
}
370.
371.
372.
}
373.
374.
public
String getHttpPost(String url,List<NameValuePair> params) {
375.
StringBuilder str =
new
StringBuilder();
376.
HttpClient client =
new
DefaultHttpClient();
377.
HttpPost httpPost =
new
HttpPost(url);
378.
379.
try
{
380.
httpPost.setEntity(
new
UrlEncodedFormEntity(params,
"UTF-8"
));
381.
HttpResponse response = client.execute(httpPost);
382.
StatusLine statusLine = response.getStatusLine();
383.
int
statusCode = statusLine.getStatusCode();
384.
if
(statusCode ==
200
) {
385.
HttpEntity entity = response.getEntity();
386.
InputStream content = entity.getContent();
387.
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(content));
388.
String line;
389.
while
((line = reader.readLine()) !=
null
) {
390.
str.append(line);
391.
}
392.
}
else
{
393.
Log.e(
"Log"
,
"Failed to download result.."
);
394.
}
395.
}
catch
(ClientProtocolException e) {
396.
e.printStackTrace();
397.
}
catch
(IOException e) {
398.
e.printStackTrace();
399.
}
400.
return
str.toString();
401.
}
402.
403.
public
void
logoutFromFacebook() {
404.
mAsyncRunner.logout(
this
,
new
RequestListener() {
405.
@Override
406.
public
void
onComplete(String response, Object state) {
407.
Log.d(
"Logout from Facebook"
, response);
408.
if
(Boolean.parseBoolean(response) ==
true
) {
409.
runOnUiThread(
new
Runnable() {
410.
411.
@Override
412.
public
void
run() {
413.
414.
}
415.
416.
});
417.
418.
}
419.
}
420.
421.
@Override
422.
public
void
onIOException(IOException e, Object state) {
423.
424.
425.
}
426.
427.
@Override
428.
public
void
onFileNotFoundException(FileNotFoundException e,
429.
Object state) {
430.
431.
432.
}
433.
434.
@Override
435.
public
void
onMalformedURLException(MalformedURLException e,
436.
Object state) {
437.
438.
439.
}
440.
441.
@Override
442.
public
void
onFacebookError(FacebookError e, Object state) {
443.
444.
445.
}
446.
447.
});
448.
}
449.
450.
451.
}