Java tutorial
package com.mobello.shift.fragments; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CompoundButton; import android.widget.RelativeLayout; import android.widget.ViewFlipper; import com.applozic.mobicomkit.api.account.user.UserClientService; import com.facebook.CallbackManager; import com.facebook.FacebookCallback; import com.facebook.FacebookException; import com.facebook.FacebookSdk; import com.facebook.share.model.AppInviteContent; import com.facebook.share.widget.AppInviteDialog; import com.kyleduo.switchbutton.SwitchButton; import com.mobello.shift.R; import com.mobello.shift.activity.HomeScreen; import com.mobello.shift.activity.InviteEmailScreen; import com.mobello.shift.activity.InvitesContactsScreen; import com.mobello.shift.activity.WebScreen; import com.mobello.shift.connectivity.Connection; import com.mobello.shift.connectivity.NetworkRequest; import com.mobello.shift.util.CommonDialog; import com.mobello.shift.util.Constant; import com.mobello.shift.util.Helper; import com.mobello.shift.util.Preference; import com.mobello.shift.widget.CustomTextView; import org.json.JSONException; import org.json.JSONObject; import java.util.HashMap; public class Settings extends Fragment implements View.OnClickListener, CompoundButton.OnCheckedChangeListener { protected Intent intent; private String token, status, appLinkUrl, previewImageUrl; private CustomTextView txtLogout, txtDeleteAccount; private RelativeLayout invite_sms, invite_email, invite_facebook, lblLicense, lblPrivacy, lblTerms, rateUs, more; protected Toolbar toolbar; private String matchNotification, likeNotification, newJobNotification; private HashMap<String, String> params; private CommonDialog dialog; private SwitchButton switchMatch, switchLikes, switchNewJobs; private boolean isLoad, isMatchTrigger, isLikesTrigger, isNewJobsTrigger; private ViewFlipper flipper; public static CustomTextView txtTitle; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_settings, container, false); try { init(rootView); appLinkUrl = "https://l.facebook.com/l.php?u=https%3A%2F%2Ffb.me%2F1212943328758597&h=IAQEOfktr"; previewImageUrl = "https://goo.gl/8NRx4B"; token = Preference.getPrefString(getActivity(), Constant.TOKEN); txtLogout.setOnClickListener(this); lblPrivacy.setOnClickListener(this); lblTerms.setOnClickListener(this); rateUs.setOnClickListener(this); txtDeleteAccount.setOnClickListener(this); invite_sms.setOnClickListener(this); lblLicense.setOnClickListener(this); invite_email.setOnClickListener(this); invite_facebook.setOnClickListener(this); switchMatch.setOnCheckedChangeListener(this); switchLikes.setOnCheckedChangeListener(this); switchNewJobs.setOnCheckedChangeListener(this); more.setOnClickListener(this); params = new HashMap<>(); isLoad = false; isMatchTrigger = false; isLikesTrigger = false; isNewJobsTrigger = false; checkNetwork(false); } catch (Exception e) { Helper.setErrorLog(getActivity(), e); } return rootView; } private void init(View rootView) { txtLogout = (CustomTextView) rootView.findViewById(R.id.txt_logout); invite_sms = (RelativeLayout) rootView.findViewById(R.id.invite_sms); invite_email = (RelativeLayout) rootView.findViewById(R.id.invite_email); invite_facebook = (RelativeLayout) rootView.findViewById(R.id.invite_facebook); lblTerms = (RelativeLayout) rootView.findViewById(R.id.lbl_terms); lblLicense = (RelativeLayout) rootView.findViewById(R.id.lbl_licenses); flipper = (ViewFlipper) rootView.findViewById(R.id.flipper); lblPrivacy = (RelativeLayout) rootView.findViewById(R.id.lbl_privacy); switchMatch = (SwitchButton) rootView.findViewById(R.id.switch_match); switchLikes = (SwitchButton) rootView.findViewById(R.id.switch_likes); switchNewJobs = (SwitchButton) rootView.findViewById(R.id.switch_new_jobs); txtDeleteAccount = (CustomTextView) rootView.findViewById(R.id.txt_delete_account); rateUs = (RelativeLayout) rootView.findViewById(R.id.rate_us); more = (RelativeLayout) rootView.findViewById(R.id.more); txtTitle = (CustomTextView) rootView.findViewById(R.id.txt_title); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.txt_logout: dialog = new CommonDialog(getActivity(), CommonDialog.Type.CUSTOM_TYPE, "LOGOUT", "Do you want to logout", "Cancel", "Ok", new CommonDialog.onClickListener() { @Override public void onNative() { dialog.dismiss(); } @Override public void onPositive() { dialog.dismiss(); getLogout(); } }); dialog.show(); break; case R.id.rate_us: try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getActivity().getPackageName()))); } catch (android.content.ActivityNotFoundException e) { startActivity(new Intent(Intent.ACTION_VIEW, Uri .parse("http://play.google.com/store/apps/details?id=" + getActivity().getPackageName()))); } break; case R.id.txt_delete_account: dialog = new CommonDialog(getActivity(), CommonDialog.Type.CUSTOM_TYPE, "Shift", "Do you want to Delete this account", "Cancel", "Ok", new CommonDialog.onClickListener() { @Override public void onNative() { dialog.dismiss(); } @Override public void onPositive() { dialog.dismiss(); deleteAccount(); } }); dialog.show(); break; case R.id.invite_facebook: facebookAppInvite(); break; case R.id.invite_email: intent = new Intent(getActivity(), InviteEmailScreen.class); startActivity(intent); break; case R.id.invite_sms: intent = new Intent(getActivity(), InvitesContactsScreen.class); startActivity(intent); break; case R.id.lbl_licenses: sendURL(getString(R.string.licenses), Constant.LICENSE_URL); break; case R.id.lbl_privacy: sendURL(getString(R.string.privacy_policy), Constant.PRIVACY_URL); break; case R.id.lbl_terms: sendURL(getString(R.string.terms_of_service), Constant.TERMS_URL); break; case R.id.more: if (txtTitle.getText().toString().equalsIgnoreCase(getString(R.string.settings))) { flipper.setInAnimation(Helper.inFromRightAnimation()); flipper.setOutAnimation(Helper.outToLeftAnimation()); flipper.showNext(); txtTitle.setText(R.string.more); } break; } } private void sendURL(String title, String url) { intent = new Intent(getActivity(), WebScreen.class); intent.putExtra(Constant.START_FROM, Constant.FRAGMENT_LICENSE); intent.putExtra(Constant.TITTLE, title); intent.putExtra(Constant.WEB_VIEW_URL, url); startActivity(intent); } private void facebookAppInvite() { if (!FacebookSdk.isInitialized()) FacebookSdk.sdkInitialize(getActivity()); CallbackManager callbackManager; callbackManager = CallbackManager.Factory.create(); if (AppInviteDialog.canShow()) { AppInviteContent content = new AppInviteContent.Builder().setApplinkUrl(appLinkUrl) .setPreviewImageUrl(previewImageUrl).build(); AppInviteDialog appInviteDialog = new AppInviteDialog(getActivity()); appInviteDialog.registerCallback(callbackManager, new FacebookCallback<AppInviteDialog.Result>() { @Override public void onSuccess(AppInviteDialog.Result result) { Log.d("Invitation", "Invitation Sent Successfully"); } @Override public void onCancel() { } @Override public void onError(FacebookException e) { Log.d("Invitation", "Error Occured"); } }); appInviteDialog.show(content); } } private void deleteAccount() { try { HashMap<String, String> param = new HashMap<>(); param.put("token", token); new NetworkRequest(getActivity(), new NetworkRequest.CallbackInterface() { @Override public void onRequestSuccess(JSONObject jsonObject) { try { String status = jsonObject.getString("status"); if (status.equalsIgnoreCase(Constant.SUCCESS)) { ((HomeScreen) getActivity()).logout(); new UserClientService(getActivity()).logout(); } else Helper.showToast(getActivity(), jsonObject.getString("error")); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onRequestFailed(NetworkRequest.ErrorType errorType) { } }, param, Constant.URL_DELETE_ACCOUNT, Constant.GET_METHOD).execute(); } catch (Exception e) { Helper.setErrorLog(getActivity(), e); } } private void getLogout() { try { HashMap<String, String> param = new HashMap<>(); param.put("token", token); new NetworkRequest(getActivity(), new NetworkRequest.CallbackInterface() { @Override public void onRequestSuccess(JSONObject jsonObject) { try { String status = jsonObject.getString("status"); if (status.equalsIgnoreCase(Constant.SUCCESS)) { ((HomeScreen) getActivity()).logout(); new UserClientService(getActivity()).logout(); } else Helper.showToast(getActivity(), jsonObject.getString("error")); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onRequestFailed(NetworkRequest.ErrorType errorType) { } }, param, Constant.URL_LOGOUT, Constant.GET_METHOD).execute(); } catch (Exception e) { Helper.setErrorLog(getActivity(), e); } } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { String value; switch (buttonView.getId()) { case R.id.switch_match: if (isChecked) value = "1"; else value = "0"; if (isMatchTrigger) { params = new HashMap<>(); params.put("match_notification", value); checkNetwork(true); } isMatchTrigger = true; break; case R.id.switch_likes: if (isChecked) value = "1"; else value = "0"; if (isLikesTrigger) { params = new HashMap<>(); params.put("like_notification", value); checkNetwork(true); } isLikesTrigger = true; break; case R.id.switch_new_jobs: if (isChecked) value = "1"; else value = "0"; if (isNewJobsTrigger) { params = new HashMap<>(); params.put("new_jobs_notification", value); checkNetwork(true); } isNewJobsTrigger = true; break; } } private void checkNetwork(boolean isProgress) { if (Connection.isAvailable(getActivity())) getNotificationSettings(params, isProgress); else Helper.showNetworkErrorDialog(getActivity()); } private void getNotificationSettings(HashMap<String, String> param, boolean isProgress) { new NetworkRequest(getActivity(), isProgress, new NetworkRequest.CallbackInterface() { @Override public void onRequestSuccess(JSONObject jsonObject) { try { status = jsonObject.getString("status"); if (status.equalsIgnoreCase(Constant.SUCCESS)) { matchNotification = jsonObject.getString("match_notification"); likeNotification = jsonObject.getString("like_notification"); newJobNotification = jsonObject.getString("new_jobs_notification"); if (!isLoad) { if (matchNotification.equalsIgnoreCase("1")) switchMatch.setChecked(true); else isMatchTrigger = true; if (likeNotification.equalsIgnoreCase("1")) switchLikes.setChecked(true); else isLikesTrigger = true; if (newJobNotification.equalsIgnoreCase("1")) switchNewJobs.setChecked(true); else isNewJobsTrigger = true; isLoad = true; } } } catch (JSONException e) { Helper.showJsonParserError(getActivity()); e.printStackTrace(); } } @Override public void onRequestFailed(NetworkRequest.ErrorType errorType) { Helper.showErrorMessage(getActivity(), errorType.toString()); } }, param, Constant.URL_SETTINGS + token, Constant.POST_METHOD).execute(); } public void hideMore() { flipper.setInAnimation(Helper.inFromLeftAnimation()); flipper.setOutAnimation(Helper.outToRightAnimation()); flipper.showPrevious(); txtTitle.setText(R.string.settings); } }