package ru.ifsoft.mynews; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.android.volley.DefaultRetryPolicy; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.RetryPolicy; import com.android.volley.VolleyError; import com.facebook.AccessToken; import com.facebook.CallbackManager; import com.facebook.FacebookCallback; import com.facebook.FacebookException; import com.facebook.GraphRequest; import com.facebook.GraphResponse; import com.facebook.login.LoginManager; import com.facebook.login.LoginResult; import com.facebook.login.widget.LoginButton; import org.json.JSONObject; import java.util.HashMap; import java.util.Map; import ru.ifsoft.mynews.app.App; import ru.ifsoft.mynews.constants.Constants; import ru.ifsoft.mynews.util.CustomRequest; import ru.ifsoft.mynews.util.Helper; public class LoginFragment extends Fragment implements Constants { CallbackManager callbackManager; LoginButton loginButton; private ProgressDialog pDialog; TextView mForgotPassword, mSignupBtn; Button signinBtn; EditText signinUsername, signinPassword; String username, password, facebookId = "", facebookName = "", facebookEmail = "", pageId = ""; private Boolean loading = false; public LoginFragment() { // Required empty public constructor } int socketTimeout = 30000; // 30 seconds. You can change it RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); if (AccessToken.getCurrentAccessToken()!= null) LoginManager.getInstance().logOut(); callbackManager = CallbackManager.Factory.create(); initpDialog(); Intent i = getActivity().getIntent(); pageId = i.getStringExtra("pageId"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_login, container, false); if (loading) { showpDialog(); } loginButton = (LoginButton) rootView.findViewById(R.id.login_button); loginButton.setReadPermissions("user_friends, email"); if (!FACEBOOK_AUTHORIZATION) { loginButton.setVisibility(View.GONE); } signinUsername = (EditText) rootView.findViewById(R.id.signinUsername); signinPassword = (EditText) rootView.findViewById(R.id.signinPassword); mForgotPassword = (TextView) rootView.findViewById(R.id.forgotPassword); mForgotPassword.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(getActivity(), RecoveryActivity.class); startActivity(i); getActivity().finish(); } }); signinBtn = (Button) rootView.findViewById(R.id.signinBtn); mSignupBtn = (TextView) rootView.findViewById(R.id.signupButton); signinBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { username = signinUsername.getText().toString(); password = signinPassword.getText().toString(); if (!App.getInstance().isConnected()) { Toast.makeText(getActivity(), R.string.msg_network_error, Toast.LENGTH_SHORT).show(); } else if (!checkUsername() || !checkPassword()) { } else { signin(); } } }); mSignupBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), SignupActivity.class); startActivity(intent); getActivity().finish(); } }); LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback() { @Override public void onSuccess(LoginResult loginResult) { // App code if (App.getInstance().isConnected()) { loading = true; showpDialog(); GraphRequest request = GraphRequest.newMeRequest( AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject object, GraphResponse response) { // Application code try { facebookId = object.getString("id"); facebookName = object.getString("name"); if (object.has("email")) { facebookEmail = object.getString("email"); } } catch (Throwable t) { Log.e("Profile", "Could not parse malformed JSON: \"" + object.toString() + "\""); } finally { if (AccessToken.getCurrentAccessToken() != null) LoginManager.getInstance().logOut(); Log.d("Profile", object.toString()); if (App.getInstance().isConnected()) { if (!facebookId.equals("")) { signinByFacebookId(); } else { loading = false; hidepDialog(); } } else { loading = false; hidepDialog(); } } } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id,name,link,email"); request.setParameters(parameters); request.executeAsync(); } } @Override public void onCancel() { // App code // Cancel } @Override public void onError(FacebookException exception) { // App code // error } }); // Inflate the layout for this fragment return rootView; } public void onDestroyView() { super.onDestroyView(); hidepDialog(); } protected void initpDialog() { pDialog = new ProgressDialog(getActivity()); pDialog.setMessage(getString(R.string.msg_loading)); pDialog.setCancelable(false); } protected void showpDialog() { if (!pDialog.isShowing()) pDialog.show(); } protected void hidepDialog() { if (pDialog.isShowing()) pDialog.dismiss(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); } public void signinByFacebookId() { CustomRequest jsonReq = new CustomRequest(Request.Method.POST, METHOD_ACCOUNT_LOGINBYFACEBOOK, null, new Response.Listener () { @Override public void onResponse(JSONObject response) { if (App.getInstance().authorize(response)) { if (App.getInstance().getState() == ACCOUNT_STATE_ENABLED) { App.getInstance().updateGeoLocation(); success(); // Intent intent = new Intent(getActivity(), MainActivity.class);// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);// startActivity(intent); } else { if (App.getInstance().getState() == ACCOUNT_STATE_BLOCKED) { App.getInstance().logout(); Toast.makeText(getActivity(), getText(R.string.msg_account_blocked), Toast.LENGTH_SHORT).show(); } else { App.getInstance().updateGeoLocation(); success(); // Intent intent = new Intent(getActivity(), MainActivity.class);// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);// startActivity(intent); } } } else { if (facebookId != "") { Intent i = new Intent(getActivity(), SignupActivity.class); i.putExtra("facebookId", facebookId); i.putExtra("facebookName", facebookName); i.putExtra("facebookEmail", facebookEmail); startActivity(i); getActivity().finish(); } else { Toast.makeText(getActivity(), getString(R.string.error_signin), Toast.LENGTH_SHORT).show(); } } loading = false; hidepDialog(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getActivity(), getText(R.string.error_data_loading), Toast.LENGTH_LONG).show(); loading = false; hidepDialog(); } }) { @Override protected Map getParams() { Map params = new HashMap (); params.put("facebookId", facebookId); params.put("clientId", CLIENT_ID); params.put("gcm_regId", App.getInstance().getGcmToken()); return params; } }; jsonReq.setRetryPolicy(policy); App.getInstance().addToRequestQueue(jsonReq); } public void signin() { loading = true; showpDialog(); CustomRequest jsonReq = new CustomRequest(Request.Method.POST, METHOD_ACCOUNT_LOGIN, null, new Response.Listener () { @Override public void onResponse(JSONObject response) { if (App.getInstance().authorize(response)) { if (App.getInstance().getState() == ACCOUNT_STATE_ENABLED) { App.getInstance().updateGeoLocation(); success(); // Intent intent = new Intent(getActivity(), MainActivity.class);// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);// startActivity(intent); } else { if (App.getInstance().getState() == ACCOUNT_STATE_BLOCKED) { App.getInstance().logout(); Toast.makeText(getActivity(), getText(R.string.msg_account_blocked), Toast.LENGTH_SHORT).show(); } else { App.getInstance().updateGeoLocation(); success(); // Intent intent = new Intent(getActivity(), MainActivity.class);// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);// startActivity(intent); } } } else { Toast.makeText(getActivity(), getString(R.string.error_signin), Toast.LENGTH_SHORT).show(); } loading = false; hidepDialog(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getActivity(), getText(R.string.error_data_loading), Toast.LENGTH_LONG).show(); loading = false; hidepDialog(); } }) { @Override protected Map getParams() { Map params = new HashMap (); params.put("username", username); params.put("password", password); params.put("clientId", CLIENT_ID); return params; } }; jsonReq.setRetryPolicy(policy); App.getInstance().addToRequestQueue(jsonReq); } public void success() { Intent i = new Intent(); i.putExtra("pageId", pageId); getActivity().setResult(getActivity().RESULT_OK, i); getActivity().finish(); } public Boolean checkUsername() { username = signinUsername.getText().toString(); signinUsername.setError(null); Helper helper = new Helper(); if (username.length() == 0) { signinUsername.setError(getString(R.string.error_field_empty)); return false; } if (username.length() < 5) { signinUsername.setError(getString(R.string.error_small_username)); return false; } if (!helper.isValidLogin(username) && !helper.isValidEmail(username)) { signinUsername.setError(getString(R.string.error_wrong_format)); return false; } return true; } public Boolean checkPassword() { password = signinPassword.getText().toString(); signinPassword.setError(null); Helper helper = new Helper(); if (username.length() == 0) { signinPassword.setError(getString(R.string.error_field_empty)); return false; } if (password.length() < 6) { signinPassword.setError(getString(R.string.error_small_password)); return false; } if (!helper.isValidPassword(password)) { signinPassword.setError(getString(R.string.error_wrong_format)); return false; } return true; } @Override public void onAttach(Activity activity) { super.onAttach(activity); } @Override public void onDetach() { super.onDetach(); } }
Wednesday, November 16, 2016
volley time out situation handler
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Dharamart.blogspot.in