Advertisement
CloudTheWolf

LoginActivity.java

Jul 27th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.96 KB | None | 0 0
  1. package com.networkstudios.app;
  2.  
  3.  
  4.  
  5.  
  6. import java.net.URI;
  7. import java.util.Map;
  8.  
  9. import android.content.Intent;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.Menu;
  13. import android.view.MenuInflater;
  14. import android.view.MenuItem;
  15. import android.view.View;
  16. import android.view.WindowManager;
  17. import android.widget.Button;
  18. import android.widget.EditText;
  19.  
  20. import com.facebook.*;
  21. import com.facebook.model.GraphUser;
  22. import com.facebook.widget.LoginButton;
  23.  
  24. public class LoginActivity extends ActivityBase {
  25.     private static final String TAG = "LoginActivity";
  26.    
  27.     protected String m_sSiteUrl;
  28.     protected LoginButton m_buttonLoginFB;
  29.     protected View m_viewLoginButtonWrapperFB;
  30.     protected Button m_buttonSubmit;   
  31.     protected EditText m_editUsername;
  32.     protected EditText m_editPassword;
  33.     protected int m_iIndex;
  34.    
  35.     private UiLifecycleHelper uiHelper;
  36.    
  37.     private Session.StatusCallback callback = new Session.StatusCallback() {
  38.         @Override
  39.         public void call(Session session, SessionState state, Exception exception) {
  40.             onSessionStateChange(session, state, exception);
  41.         }
  42.     };
  43.    
  44.     private LoginActionCallback m_oCallback = new LoginActionCallback(this) {      
  45.         public boolean callFailed(Exception e) {
  46.             if (e.getMessage().endsWith("[code 1]") && "dolphin.login4" == sMethod) { // method dolphin.login4 not found
  47.                 Log.d(TAG, "new protocol dolphin.login4 function not found, using old protocol dolphin.login function");
  48.                 Object[] aParams = m_oCallback.getParams();
  49.                 aParams[1] = oConnector.md5((String)aParams[1]);
  50.                 setMethod("dolphin.login2");  
  51.                 oConnector.execAsyncMethod("dolphin.login2", aParams, this, context);
  52.                 return false;
  53.             } else if (e.getMessage().endsWith("[code 1]") && "dolphin.login2" == sMethod) { // method dolphin.login2 not found
  54.                 Log.d(TAG, "new protocol dolphin.login2 function not found, using old protocol dolphin.login function");
  55.                 setMethod("dolphin.login");  
  56.                 oConnector.execAsyncMethod("dolphin.login", aParams, this, context);
  57.                 return false;
  58.             } else {
  59.                 return true;
  60.             }
  61.         }
  62.         public void callFinished(Object result) {
  63.             Log.d(TAG, "dolphin.login result: " + result.toString() + " / class: " + result.getClass());
  64.            
  65.             int iIdProfile;
  66.             int iProtocolVer;
  67.             String sProfileUsername = null;
  68.             String sProfilePwdHash = null;
  69.            
  70.             if (result instanceof Map) {
  71.                 @SuppressWarnings("unchecked")
  72.                 Map<String, Object> map = (Map<String, Object>)result;
  73.                
  74.                 if (map.get("member_id") instanceof String)
  75.                     iIdProfile = Integer.parseInt((String)map.get("member_id"));
  76.                 else
  77.                     iIdProfile = (Integer)map.get("member_id");
  78.                
  79.                 if (map.get("protocol_ver") instanceof String)
  80.                     iProtocolVer = Integer.parseInt((String)map.get("protocol_ver"));
  81.                 else
  82.                     iProtocolVer = (Integer)map.get("protocol_ver");
  83.                
  84.                 if (iProtocolVer >= 4) {
  85.                     sProfileUsername = (String)map.get("member_username");
  86.                     sProfilePwdHash = (String)map.get("member_pwd_hash");
  87.                 }
  88.             } else {
  89.                 iIdProfile = Integer.valueOf(result.toString());
  90.                 iProtocolVer = 1;
  91.             }
  92.            
  93.             Log.d(TAG, "MEMBER ID: " + iIdProfile);
  94.             Log.d(TAG, "PROTOCOL VERSION: " + iProtocolVer);
  95.            
  96.             if (iIdProfile > 0) {
  97.                
  98.                 Bundle b = new Bundle();
  99.                 b.putInt("index", m_iIndex);
  100.                 b.putString("site", correctSiteUrl(m_sSiteUrl));
  101.                 b.putInt("member_id", iIdProfile);
  102.                 b.putString("username", sProfileUsername != null ? sProfileUsername : m_editUsername.getText().toString());
  103.                 b.putString("password", sProfilePwdHash != null ? sProfilePwdHash : m_editPassword.getText().toString());                          
  104.                 b.putBoolean("remember_password", true);
  105.                 b.putInt("protocol", iProtocolVer);
  106.                 Intent i = new Intent();
  107.                 i.putExtras(b);
  108.                
  109.                 setResult(RESULT_LOGIN, i);
  110.            
  111.                 finish();
  112.                
  113.             } else {
  114.  
  115.                 alertError (R.string.msg_login_incorrect);
  116.             }
  117.         }
  118.     };
  119.    
  120.     // result codes
  121.     public static final int RESULT_DELETE = RESULT_FIRST_USER + 1;
  122.     public static final int RESULT_LOGIN = RESULT_FIRST_USER + 2;
  123.     public static final int RESULT_ADD = RESULT_FIRST_USER + 3;
  124.    
  125.     @Override
  126.     protected void onCreate(Bundle b) {
  127.         super.onCreate(b, true, false, false);
  128.        
  129.         uiHelper = new UiLifecycleHelper(this, callback);
  130.         uiHelper.onCreate(b);
  131.        
  132.         setContentView(R.layout.login);
  133.                        
  134.         m_editUsername = (EditText) findViewById(R.id.username);
  135.         m_editPassword = (EditText) findViewById(R.id.password);        
  136.         m_buttonSubmit = (Button) findViewById(R.id.submit);        
  137.         m_buttonLoginFB = (LoginButton) findViewById(R.id.fb_login_button);
  138.         m_viewLoginButtonWrapperFB = (View) findViewById(R.id.fb_login_button_wrapper);
  139.        
  140.         Intent i = getIntent();
  141.         m_sSiteUrl = i.getStringExtra("site");
  142.         m_iIndex = i.getIntExtra("index", 0);
  143.         m_editUsername.setText(i.getStringExtra("username"));
  144.         m_editPassword.setText(i.getStringExtra("password"));          
  145.         m_buttonSubmit.setText(R.string.title_login);
  146.                                            
  147.         String sCaption = m_sSiteUrl.replace("http://", "").replace("/xmlrpc", "");
  148.         if (sCaption.endsWith("/"))
  149.             sCaption = sCaption.substring(0, sCaption.length()-1);
  150.         setTitleCaption (sCaption);
  151.        
  152.         View.OnClickListener listener = new View.OnClickListener() {           
  153.             public void onClick(View view) {               
  154.                                
  155.                 Object[] aParams = {
  156.                     m_editUsername.getText().toString(),
  157.                     m_editPassword.getText().toString()
  158.                 };                    
  159.                                
  160.                 actionLogin("dolphin.login4", aParams);
  161.             }          
  162.         };                
  163.         m_buttonSubmit.setOnClickListener(listener);
  164.  
  165.         hideKeyboard();
  166.        
  167.         if (m_editPassword.getText().toString().length() > 0) { // try autologin if password saved
  168.             Object[] aParams = {
  169.                     m_editUsername.getText().toString(),
  170.                     m_editPassword.getText().toString()
  171.             };                    
  172.  
  173.             actionLogin("dolphin.login4", aParams);
  174.         } else {
  175.             checkCompatibilityWithFb();
  176.         }        
  177.     }
  178.    
  179.      
  180.     protected void hideKeyboard() {
  181.         this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
  182.     }
  183.  
  184.     @Override
  185.     protected void onDestroy() {       
  186.         super.onDestroy();
  187.         uiHelper.onDestroy();
  188.     }
  189.    
  190.     @Override
  191.     protected void onPause() {     
  192.         super.onPause();
  193.         uiHelper.onPause();
  194.     }
  195.  
  196.     @Override
  197.     protected void onResume() {    
  198.         super.onResume();
  199.         uiHelper.onResume();       
  200.     }
  201.  
  202.     @Override
  203.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
  204.       super.onActivityResult(requestCode, resultCode, data);
  205.       uiHelper.onActivityResult(requestCode, resultCode, data);
  206.     }
  207.    
  208.     @Override
  209.     protected void onSaveInstanceState(Bundle outState) {
  210.         super.onSaveInstanceState(outState);       
  211.         uiHelper.onSaveInstanceState(outState);
  212.     }
  213.    
  214.     @Override
  215.     public boolean onCreateOptionsMenu(Menu menu) {
  216.         if (m_iIndex >= 0 && Main.LOCK_TO_SITE == null) {
  217.             MenuInflater inflater = getMenuInflater();
  218.             inflater.inflate(R.menu.login, menu);
  219.             return true;
  220.         }
  221.         return false;
  222.     }
  223.    
  224.     @Override
  225.     public boolean onOptionsItemSelected(MenuItem item) {
  226.         switch (item.getItemId()) {
  227.         case R.id.login_delete_site:
  228.             Bundle b = new Bundle();
  229.             b.putInt("index", m_iIndex);                           
  230.             Intent i = new Intent();
  231.             i.putExtras(b);
  232.             setResult(RESULT_DELETE, i);
  233.             finish();
  234.             break;
  235.         }
  236.         return super.onOptionsItemSelected(item);
  237.     }
  238.    
  239.     private void onSessionStateChange(Session session, SessionState state, Exception exception) {        
  240.     }
  241.        
  242.     protected void actionLogin(String sMethod, Object[] aParams) {             
  243.        
  244.         String sCorrectUrl = correctSiteUrl(m_sSiteUrl);
  245.        
  246.         try {
  247.             URI.create(sCorrectUrl);
  248.         } catch (IllegalArgumentException e) {         
  249.             alertError(R.string.msg_url_incorrect);
  250.             return;
  251.         }
  252.        
  253.         Connector o = new Connector (sCorrectUrl, m_editUsername.getText().toString(), m_editPassword.getText().toString(), 0);
  254.                
  255.         m_oCallback.setMethod(sMethod);
  256.         m_oCallback.setParams(aParams);
  257.         m_oCallback.setConnector(o);
  258.         o.execAsyncMethod(sMethod, aParams, m_oCallback, m_actThis);
  259.     }
  260.  
  261.     protected void checkCompatibilityWithFb () {
  262.        
  263.         String sMethod = "dolphin.service";    
  264.         String sCorrectUrl = correctSiteUrl(m_sSiteUrl);
  265.                
  266.         Connector o = new Connector (sCorrectUrl, "", "", 0);
  267.        
  268.         Object[] aEmptyArray = {};
  269.         Object[] aParams = {"facebook_connect", "supported", aEmptyArray, "Module"};
  270.        
  271.         Connector.Callback oCallback = new Connector.Callback() {
  272.            
  273.             public boolean callFailed(Exception e) {               
  274.                 return false;
  275.             }
  276.            
  277.             public void callFinished(Object result) {
  278.                 Log.d(TAG, "dolphin.service result: " + result + " / class: " + result.getClass());
  279.                
  280.                 try {
  281.                     if (!(result instanceof String) || 1 != Integer.parseInt((String)result))
  282.                         return;
  283.                 } catch (NumberFormatException e) {
  284.                     Log.d(TAG, "dolphin.service execption: " + e);
  285.                     return;
  286.                 }
  287.                    
  288.                 m_viewLoginButtonWrapperFB.setVisibility(View.VISIBLE);
  289.                 m_buttonLoginFB.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
  290.                     @Override
  291.                     public void onUserInfoFetched(GraphUser user) {
  292.                         if (null == user)
  293.                             return;
  294.  
  295.                         Object[] aUser = {user.asMap()};
  296.                         Object[] aParams = {
  297.                                 "facebook_connect",
  298.                                 "login",
  299.                                 aUser,
  300.                                 "Module"
  301.                         };                    
  302.  
  303.                         actionLogin("dolphin.service", aParams);            
  304.                     }
  305.                 });
  306.  
  307.             }
  308.         };
  309.        
  310.         Log.i(TAG, sMethod + ": " + aParams[2]);
  311.        
  312.         o.execAsyncMethod(sMethod, aParams, oCallback, m_actThis);
  313.     }
  314.    
  315.     class LoginActionCallback extends Connector.Callback {
  316.         protected LoginActivity context;
  317.         protected String sMethod;
  318.         protected Object[] aParams;
  319.         protected Connector oConnector;
  320.         public LoginActionCallback (LoginActivity c) {
  321.             context = c;
  322.         }
  323.         public void setMethod(String s) {
  324.             sMethod = s;
  325.         }
  326.         public void setParams(Object[] a) {
  327.             aParams = a;
  328.         }
  329.         public Object[] getParams() {
  330.             return aParams;
  331.         }      
  332.         public void setConnector(Connector o) {
  333.             oConnector = o;
  334.         }      
  335.     }
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement