Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. import org.json.JSONException;
  4. import org.json.JSONObject;
  5.  
  6. import android.app.Activity;
  7. import android.app.ProgressDialog;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.SharedPreferences;
  11. import android.os.AsyncTask;
  12. import android.os.Bundle;
  13. import android.telephony.TelephonyManager;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.view.View.OnClickListener;
  17. import android.view.View.OnTouchListener;
  18. import android.view.Window;
  19. import android.widget.Button;
  20. import android.widget.Toast;
  21.  
  22. import com.dobango.finalapp.games.CocosGames;
  23. import com.dobango.finalapp.util.Common;
  24. import com.dobango.finalapp.util.services.FBRegistrationService;
  25. import com.facebook.CallbackManager;
  26. import com.facebook.FacebookCallback;
  27. import com.facebook.FacebookException;
  28. import com.facebook.GraphRequest;
  29. import com.facebook.GraphResponse;
  30. import com.facebook.login.LoginManager;
  31. import com.facebook.login.LoginResult;
  32. import com.facebook.login.widget.LoginButton;
  33. import com.facebook.oldandroid.DialogError;
  34. import com.facebook.oldandroid.Facebook;
  35. import com.facebook.oldandroid.Facebook.DialogListener;
  36. import com.facebook.oldandroid.FacebookError;
  37. import com.facebook.oldandroid.Util;
  38.  
  39. import java.util.Arrays;
  40.  
  41. public class MyAccount1_1Activity extends Activity implements OnClickListener {
  42.  
  43. // Facebook facebook = new Facebook(DobangoApp.FB_KEY);
  44. private CallbackManager callbackManager;
  45. private SharedPreferences mPrefs;
  46.  
  47. Button signup,loginButton;
  48. LoginButton facebooklogin;
  49.  
  50. String DeviceID=null;
  51.  
  52. String facebookID,firstName,lastName,fbEmail,fbMessage,fbsessionID;
  53. int fbCode;
  54. //String[] detail = null;
  55. private Context cCon;
  56. //private FancyBottomTab fancyBottomBtnView;
  57. private OnTouchListener touchListener;
  58. SharedPreferences signupSessionIDpreferences;
  59. String error;
  60.  
  61. /** Called when the activity is first created. */
  62.  
  63. public void onCreate(Bundle savedInstanceState) {
  64. super.onCreate(savedInstanceState);
  65. requestWindowFeature(Window.FEATURE_NO_TITLE);
  66. /* String submitDetail = getIntent().getExtras().getString("submitDetail");
  67. detail = submitDetail.split(",");*/
  68.  
  69. /*if(Common.screenDimenSet && (Common.HEIGHT > 1200)){
  70. setContentView(R.layout.my_account1_large);
  71. }else if(!Common.screenDimenSet){*/
  72. Common.HEIGHT = getWindow().getWindowManager().getDefaultDisplay().getHeight();
  73. Common.WIDTH = getWindow().getWindowManager().getDefaultDisplay().getWidth();
  74. Common.screenDimenSet = true;
  75. /*if(Common.HEIGHT > 1200){
  76. setContentView(R.layout.my_account1_large);
  77. }else
  78. setContentView(R.layout.my_account1);
  79. }else*/
  80. setContentView(R.layout.my_account1);
  81. signupSessionIDpreferences=getSharedPreferences("sessionid",0);
  82. /*touchListener = new OnTouchListener(){
  83. public boolean onTouch(View v, MotionEvent event) {
  84. if(fancyBottomBtnView.showingItems){
  85. return true;
  86. }else{
  87. return v.onTouchEvent(event);
  88. }
  89. }
  90.  
  91. };*/
  92. cCon=this;
  93. //fancyBottomBtnView = (FancyBottomTab) findViewById(R.id.fancyBottomTab1);
  94.  
  95. mPrefs = getSharedPreferences(DobangoApp.APP_PREFS, 0);
  96. facebooklogin=(LoginButton) findViewById(R.id.facebooklogin);
  97.  
  98. //facebooklogin.setBackgroundResource(R.drawable.btn_facebook_login);
  99. //facebooklogin.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
  100.  
  101. facebooklogin.setOnTouchListener(touchListener);
  102. signup =(Button) findViewById(R.id.loginsignup);
  103. signup.setOnTouchListener(touchListener);
  104. loginButton =(Button) findViewById(R.id.loginlogin);
  105. loginButton.setOnTouchListener(touchListener);
  106.  
  107. facebooklogin.setOnClickListener(this);
  108. signup.setOnClickListener(this);
  109. loginButton.setOnClickListener(this);
  110.  
  111. TelephonyManager tManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
  112. DeviceID = tManager.getDeviceId();
  113.  
  114. }
  115.  
  116.  
  117. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  118. super.onActivityResult(requestCode, resultCode, data);
  119.  
  120.  
  121. if(requestCode==100){
  122. if(resultCode==RESULT_OK){
  123. setResult(RESULT_OK, (new Intent()).setAction("close"));
  124. this.finish();
  125. }
  126. else{
  127. this.finish();
  128. }
  129. }
  130. else{
  131. // facebook.authorizeCallback(requestCode, resultCode, data);
  132. callbackManager.onActivityResult(requestCode, resultCode, data);
  133. }
  134. }
  135.  
  136.  
  137. public void onClick(View v) {
  138.  
  139. if(v.equals(facebooklogin)){
  140.  
  141. // facebookFunction();
  142.  
  143. newFacebookFunction();
  144. }
  145.  
  146. if(v.equals(signup)){
  147. Intent intent =new Intent(MyAccount1_1Activity.this,Signup1_1Activity.class);
  148. startActivityForResult(intent, 100);
  149. }
  150.  
  151. if(v.equals(loginButton)){
  152.  
  153. Intent intent =new Intent(MyAccount1_1Activity.this,Login1_1Activity.class);
  154. startActivityForResult(intent, 100);
  155. }
  156. }
  157.  
  158. public void newFacebookFunction() {
  159. Log.e("dobango" , "New Facebook function for Login");
  160. callbackManager = CallbackManager.Factory.create();
  161.  
  162. LoginManager.getInstance().logInWithReadPermissions(MyAccount1_1Activity.this, Arrays.asList("public_profile", "user_friends", "email"));
  163. facebooklogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
  164. @Override
  165. public void onSuccess(LoginResult loginResult) {
  166.  
  167. Log.e("onSuccess", "--------" + loginResult.getAccessToken());
  168. Log.e("Token", "--------" + loginResult.getAccessToken().getToken());
  169. Log.e("Permision", "--------" + loginResult.getRecentlyGrantedPermissions());
  170.  
  171. GraphRequest request = GraphRequest.newMeRequest(
  172. loginResult.getAccessToken(),
  173. new GraphRequest.GraphJSONObjectCallback() {
  174. @Override
  175. public void onCompleted(
  176. JSONObject object,
  177. GraphResponse response) {
  178. //Toast.makeText(Signup1_1Activity.this, "User Profile--- " + response.toString(), Toast.LENGTH_LONG).show();
  179. Log.e("facebook" , response.getJSONObject().toString());
  180. try {
  181. firstName = (String) response.getJSONObject().get("name");
  182. fbEmail = (String) response.getJSONObject().get("email");
  183. facebookID = (String) response.getJSONObject().get("id");
  184.  
  185. Log.e("facebook" , firstName + " " + fbEmail + " " + facebookID);
  186.  
  187. } catch(JSONException ex) {
  188. Log.e("facebook" , "Parse Error" , ex);
  189. } finally {
  190. new AddTaskFbRegister().execute();
  191. }
  192.  
  193. }
  194. });
  195. Bundle parameters = new Bundle();
  196. parameters.putString("fields", "id,name,link,gender,birthday,email");
  197. request.setParameters(parameters);
  198. request.executeAsync();
  199.  
  200. }
  201. @Override
  202. public void onCancel() {
  203. Toast.makeText(cCon, "Login cancelled by user!", Toast.LENGTH_LONG).show();
  204. System.out.println("Facebook Login failed!!");
  205. }
  206. @Override
  207. public void onError(FacebookException e) {
  208. Toast.makeText(cCon, "Login unsuccessful!", Toast.LENGTH_LONG).show();
  209. System.out.println("Facebook Login failed!!");
  210. }
  211. });
  212. }
  213.  
  214. /* public void facebookFunction(){
  215.  
  216. mPrefs = getPreferences(MODE_PRIVATE);
  217. final String access_token = mPrefs.getString("access_token", null);
  218. long expires = mPrefs.getLong("access_expires", 0);
  219. if(access_token != null) {
  220. facebook.setAccessToken(access_token);
  221. }
  222. if(expires != 0) {
  223. facebook.setAccessExpires(expires);
  224. }
  225.  
  226. if(!facebook.isSessionValid()) {
  227.  
  228. facebook.authorize(this, DobangoApp.FB_PERMISSIONS, new DialogListener() {
  229.  
  230. //
  231. public void onFacebookError(FacebookError e) {
  232. Log.d("FACEBOOK ERROR","FB ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
  233.  
  234. }
  235.  
  236. //
  237. public void onError(DialogError e) {
  238. Log.e("ERROR","AUTH ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
  239. }
  240.  
  241. //
  242. public void onComplete(Bundle values) {
  243.  
  244. SharedPreferences.Editor editor = mPrefs.edit();
  245. editor.putString("access_token", facebook.getAccessToken());
  246. editor.putLong("access_expires", facebook.getAccessExpires());
  247. editor.commit();
  248. //control comes here if the login was successful
  249. //Facebook.TOKEN is the key by which the value of access token is stored in the Bundle called 'values'
  250. Log.d("COMPLETE","AUTH COMPLETE. VALUES: "+values.size());
  251. Log.d("AUTH TOKEN","== "+values.getString(Facebook.TOKEN));
  252. //updateRegistration(values.getString(Facebook.TOKEN));
  253. new AddFacebookLogin(access_token).execute();
  254. }
  255.  
  256. //
  257. public void onCancel() {
  258.  
  259. Log.d("CANCELLED","AUTH CANCELLED");
  260. }
  261. });
  262. }
  263.  
  264. else{
  265.  
  266. //updateRegistration(access_token);
  267. new AddFacebookLogin(access_token).execute();
  268. }
  269. }*/
  270.  
  271. /*private void updateRegistration(String accessToken){
  272.  
  273. Bundle bundle = new Bundle();
  274. bundle.putString(Facebook.TOKEN,accessToken);
  275.  
  276. try {
  277. JSONObject json = Util.parseJson(facebook.request("me"));
  278. facebookID = json.getString("id");
  279. firstName = json.getString("first_name");
  280. lastName = json.getString("last_name");
  281. fbEmail = json.getString("email");
  282.  
  283. System.out.println("facebook id:::"+facebookID);
  284. System.out.println("first_name:::"+firstName);
  285. System.out.println("last_name:::"+lastName);
  286. System.out.println("email::"+fbEmail);
  287. }
  288.  
  289. catch( Exception e ) {
  290. this.error = e.toString();
  291. this.runOnUiThread(new Runnable(){
  292. public void run() {
  293. Toast.makeText( MyAccount1_1Activity.this, error, Toast.LENGTH_SHORT).show();
  294. }
  295. });
  296. }
  297. finally{
  298. new AddTaskFbRegister().execute();
  299. }
  300. }*/
  301.  
  302. private class AddTaskFbRegister extends AsyncTask<Void, Void, Void> {
  303.  
  304. ProgressDialog pDialog;
  305.  
  306.  
  307. protected void onPreExecute() {
  308.  
  309. runOnUiThread(new Runnable(){
  310. public void run() {
  311. pDialog = new ProgressDialog(cCon);
  312. pDialog.setMessage("Please wait..");
  313. pDialog.setIndeterminate(true);
  314. pDialog.setCancelable(false);
  315. pDialog.show();
  316. }
  317. });
  318. }
  319.  
  320.  
  321. protected Void doInBackground(Void... unused) {
  322.  
  323. Log.v("FB Signup Parameter", ""+facebookID+","+fbEmail+","+DeviceID);
  324. checkRegistration();
  325. return (null);
  326. }
  327.  
  328.  
  329. protected void onProgressUpdate(Void... values) {
  330. }
  331.  
  332.  
  333. protected void onPostExecute(Void unused) {
  334.  
  335. pDialog.dismiss();
  336. showRegistrationResult();
  337. }
  338. }
  339.  
  340.  
  341. public void checkRegistration() {
  342.  
  343. String response = FBRegistrationService.FBRegister(fbEmail,facebookID);
  344. //String response = null;
  345. System.out.println("FB Signup Respone"+ response);
  346.  
  347. /*try {
  348. JsonparserLogin.Loginstringparser(response);
  349. }
  350. catch (JSONException e1) {
  351. e1.printStackTrace();
  352. }*/
  353. try {
  354. JSONObject jsonUser=new JSONObject(response);
  355. fbCode= jsonUser.getInt("code");
  356. fbMessage= jsonUser.getString("message");
  357. fbsessionID=jsonUser.getString("session");
  358. System.out.println(fbsessionID);
  359. SharedPreferences.Editor editor = signupSessionIDpreferences.edit();
  360.  
  361. editor.putString("sessionidFacebook",fbsessionID);
  362. editor.putString("sessionidLogin","");
  363. editor.commit();
  364. } catch (JSONException e) {
  365. // TODO Auto-generated catch block
  366. e.printStackTrace();
  367. }
  368. }
  369.  
  370. public void showRegistrationResult(){
  371.  
  372. if(fbCode==1000){
  373. DobangoApp.setLogin(true);
  374. if(fbEmail != null && fbEmail.length() != 0){
  375. DobangoApp.setUserName(fbEmail);
  376. }else{
  377. DobangoApp.setFacebook(firstName+" "+lastName);
  378. }
  379. DobangoApp.setFacebookId(facebookID);
  380.  
  381. SharedPreferences.Editor editor = mPrefs.edit();
  382. editor.putBoolean("isFacebookLogin", true);
  383. editor.commit();
  384.  
  385. Toast.makeText(cCon, fbMessage, Toast.LENGTH_LONG);
  386. Intent i = getIntent(); //gets the intent that called this intent
  387. setResult(Activity.RESULT_OK, i);
  388. //setResult(RESULT_OK, (new Intent()).setAction("close"));
  389. this.finish();
  390. }
  391. else{
  392. Toast.makeText(cCon, fbMessage, Toast.LENGTH_LONG);
  393. }
  394. }
  395.  
  396. public class AddFacebookLogin extends AsyncTask<Void, Void, Void> {
  397. String access_token;
  398. private AddFacebookLogin(String token){this.access_token=token;}
  399. protected void onPreExecute() {}
  400.  
  401.  
  402. protected Void doInBackground(Void... unused) {
  403. //facebookFunction();
  404. // updateRegistration(access_token);
  405.  
  406. return (null);
  407. }
  408.  
  409.  
  410. protected void onProgressUpdate(Void... values) {}
  411. protected void onPostExecute(Void unused) {}
  412. }
  413.  
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement