Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. Bundle params = new Bundle();
  2. //params.putString("message", "I want to post this text only.");
  3. params.putString("name", "Facebook SDK for Android");
  4. params.putString("caption", "Build great social apps and get more installs.");
  5. params.putString("description", "Description");
  6. params.putString("link", "https://developers.facebook.com/android");
  7. params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
  8.  
  9. WebDialog feedDialog = (
  10. new WebDialog.FeedDialogBuilder(getActivity(),
  11. Session.getActiveSession(),
  12. params))
  13. .setOnCompleteListener(new OnCompleteListener() {
  14.  
  15. if (facebook.getAccessToken() != null) {
  16.  
  17. postOnWall("msg to share");
  18.  
  19. } else {
  20. loginToFacebook();
  21.  
  22. }
  23.  
  24. public void postOnWall(String msg) {
  25. Log.d("Tests", "Testing graph API wall post");
  26. try {
  27. String response = facebook.request("me");
  28. Bundle parameters = new Bundle();
  29.  
  30. parameters.putString("message", msg);
  31. parameters.putString("description", "test test test");
  32. // parameters.putByteArray("message", msg);
  33. response = facebook.request("me/feed", parameters, "POST");
  34. Log.d("Tests", "got response: " + response);
  35. if (response == null || response.equals("")
  36. || response.equals("false")) {
  37. Log.v("Error", "Blank response");
  38.  
  39. Toast.makeText(getApplicationContext(), "FacebookError",Toast.LENGTH_SHORT).show();
  40.  
  41. }
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44.  
  45. }
  46. }
  47.  
  48. public void loginToFacebook() {
  49.  
  50. mPrefs = getPreferences(MODE_PRIVATE);
  51. String access_token = mPrefs.getString("access_token", null);
  52. long expires = mPrefs.getLong("access_expires", 0);
  53.  
  54. if (access_token != null) {
  55. facebook.setAccessToken(access_token);
  56.  
  57. Log.d("FB Sessions", "" + facebook.isSessionValid());
  58. }
  59.  
  60. if (expires != 0) {
  61. facebook.setAccessExpires(expires);
  62. }
  63.  
  64. if (!facebook.isSessionValid()) {
  65. facebook.authorize(this,
  66. new String[] { "email", "publish_stream" },
  67. new DialogListener() {
  68.  
  69. @Override
  70. public void onCancel() {
  71. // Function to handle cancel event
  72. }
  73.  
  74. @Override
  75. public void onComplete(Bundle values) {
  76. // Function to handle complete event
  77. // Edit Preferences and update facebook acess_token
  78. SharedPreferences.Editor editor = mPrefs.edit();
  79. editor.putString("access_token",
  80. facebook.getAccessToken());
  81. editor.putLong("access_expires",
  82. facebook.getAccessExpires());
  83. editor.commit();
  84. Toast.makeText(sms_by_id.this,
  85. "Successfully Login", Toast.LENGTH_LONG)
  86. .show();
  87. pwindo.dismiss();
  88.  
  89. // Making Login button invisible
  90.  
  91. }
  92.  
  93. @Override
  94. public void onError(DialogError error) {
  95. // Function to handle error
  96.  
  97. }
  98.  
  99. @Override
  100. public void onFacebookError(FacebookError fberror) {
  101. // Function to handle Facebook errors
  102. pwindo.dismiss();
  103. Toast.makeText(sms_by_id.this, "Facebook Error",
  104. Toast.LENGTH_LONG).show();
  105. }
  106.  
  107. });
  108. }
  109. }
  110.  
  111. public class ShareOnFacebook extends Activity{
  112.  
  113. private static final String APP_ID = "12dfgdfgf";
  114. private static final String[] PERMISSIONS = new String[] {"publish_stream"};
  115.  
  116. private static final String TOKEN = "access_token";
  117. private static final String EXPIRES = "2040";
  118. private static final String KEY = "gfdfdfdfdfd;
  119.  
  120. private Facebook facebook;
  121. private String messageToPost;
  122.  
  123. public boolean saveCredentials(Facebook facebook) {
  124. Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
  125. editor.putString(TOKEN, facebook.getAccessToken());
  126. editor.putLong(EXPIRES, facebook.getAccessExpires());
  127. return editor.commit();
  128. }
  129.  
  130. public boolean restoreCredentials(Facebook facebook) {
  131. SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
  132. facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
  133. facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
  134. return facebook.isSessionValid();
  135. }
  136.  
  137. @Override
  138. protected void onCreate(Bundle savedInstanceState) {
  139. super.onCreate(savedInstanceState);
  140.  
  141. facebook = new Facebook(APP_ID);
  142. restoreCredentials(facebook);
  143.  
  144. requestWindowFeature(Window.FEATURE_NO_TITLE);
  145.  
  146. setContentView(R.layout.facebook_dialog);
  147.  
  148. facebook = new Facebook(APP_ID);
  149. restoreCredentials(facebook);
  150.  
  151.  
  152. String facebookMessage = getIntent().getStringExtra("facebookMessage");
  153. if (facebookMessage == null){
  154. facebookMessage = "Test wall post";
  155. }
  156. messageToPost = facebookMessage;
  157. }
  158.  
  159. public void doNotShare(View button){
  160. finish();
  161. }
  162.  
  163.  
  164. public void share(View button){
  165. if (! facebook.isSessionValid()) {
  166. loginAndPostToWall();
  167. }
  168. else {
  169. postToWall(messageToPost);
  170. /*Intent backhome = new Intent(ShareOnFacebook.this,Quotes_Tab.class);
  171. startActivity(backhome);*/
  172. }
  173. }
  174.  
  175. public void loginAndPostToWall(){
  176. facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
  177. }
  178.  
  179. public void postToWall(String message){
  180. Bundle parameters = new Bundle();
  181. parameters.putString("message", message);
  182. // parameters.putString("link", "http://theaterbalcony.com/tag/torpedo-3d-malayalam-cartoon/");
  183.  
  184. try {
  185. facebook.request("me");
  186. String response = facebook.request("me/feed", parameters, "POST");
  187. Log.d("Tests", "got response: " + response);
  188. if (response == null || response.equals("") || response.equals("false")) {
  189. showToast("Blank response.");
  190. }
  191. else {
  192. showToast("Message posted to your facebook wall!");
  193. }
  194. finish();
  195. } catch (Exception e) {
  196. showToast("Failed to post to wall!");
  197. e.printStackTrace();
  198. finish();
  199. }
  200.  
  201. }
  202.  
  203. class LoginDialogListener implements DialogListener {
  204. public void onComplete(Bundle values) {
  205. saveCredentials(facebook);
  206. if (messageToPost != null){
  207. postToWall(messageToPost);
  208. }
  209. }
  210. public void onFacebookError(FacebookError error) {
  211. showToast("Authentication with Facebook failed!");
  212. finish();
  213. }
  214. public void onError(DialogError error) {
  215. showToast("Authentication with Facebook failed!");
  216. finish();
  217. }
  218. public void onCancel() {
  219. showToast("Authentication with Facebook cancelled!");
  220. finish();
  221. }
  222. }
  223.  
  224. private void showToast(String message){
  225. Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
  226. }
  227.  
  228. @Override
  229. protected void onDestroy() {
  230. // TODO Auto-generated method stub
  231. super.onDestroy();
  232. finish();
  233. }
  234.  
  235. @Override
  236. public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent) {
  237. // if menu button is pressed
  238. if (pKeyCode == KeyEvent.KEYCODE_MENU
  239. && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
  240.  
  241. Intent home = new Intent(ShareOnFacebook.this, Quotes_Tab.class);
  242. finish();
  243. startActivity(home);
  244. } else if (pKeyCode == KeyEvent.KEYCODE_BACK
  245. && pEvent.getAction() == KeyEvent.ACTION_DOWN) {
  246.  
  247.  
  248. Intent home = new Intent(ShareOnFacebook.this,Quotes_Tab.class);
  249. finish();
  250. startActivity(home);
  251. return super.onKeyDown(pKeyCode, pEvent);
  252. }
  253. return super.onKeyDown(pKeyCode, pEvent);
  254. }
  255. @Override
  256. protected void onStop() {
  257. // TODO Auto-generated method stub
  258. super.onStop();
  259. finish();
  260. }
  261.  
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement