Java tutorial
/* * Copyright 2016 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package dheeraj.com.trafficsolution; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.NavUtils; import android.util.Log; import android.view.KeyEvent; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.firebase.auth.AuthResult; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.ValueEventListener; import com.mikhaellopez.circularimageview.CircularImageView; import java.util.HashMap; import java.util.Map; import dheeraj.com.trafficsolution.Activities.LoginRegisterChoose; public class ProfileActivity extends BaseActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener { private static final String TAG = "ProfileActivity"; private ViewGroup mProfileUi; private FirebaseAuth mAuth; private CircularImageView mProfilePhoto; private TextView mProfileUsername; private TextView points; private static final int RC_SIGN_IN = 103; public static final String FireBaseSharedPref = "FireBaseSharedPref"; public static final String FireBaseShared_KEY = "FireBaseShared_KEY"; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); } void init() { setContentView(R.layout.activity_profile); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); getSupportActionBar().setDisplayHomeAsUpEnabled(true); mAuth = FirebaseAuth.getInstance(); mProfileUi = (ViewGroup) findViewById(R.id.profile); points = (TextView) findViewById(R.id.points); mProfilePhoto = (CircularImageView) findViewById(R.id.profile_user_photo); mProfileUsername = (TextView) findViewById(R.id.profile_user_name); findViewById(R.id.show_feeds_button).setOnClickListener(this); findViewById(R.id.sign_out_button).setOnClickListener(this); showSignedInUI(mAuth.getCurrentUser()); } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.sign_out_button: mAuth.signOut(); showSignedOutUI(); SharedPreferences sharedpreferences = getSharedPreferences(FireBaseSharedPref, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putBoolean(FireBaseShared_KEY, false); editor.commit(); break; case R.id.show_feeds_button: Intent feedsIntent = new Intent(this, FeedsActivity.class); startActivity(feedsIntent); break; } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar's Up/Home button case android.R.id.home: startActivity(new Intent(ProfileActivity.this, FeedsActivity.class)); finish(); } return super.onOptionsItemSelected(item); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); } private void handleFirebaseAuthResult(AuthResult result) { // TODO: This auth callback isn't being called after orientation change. Investigate. dismissProgressDialog(); if (result != null) { Log.d(TAG, "handleFirebaseAuthResult:SUCCESS"); showSignedInUI(result.getUser()); } else { Toast.makeText(this, "Authentication failed.", Toast.LENGTH_SHORT).show(); showSignedOutUI(); } } private void showSignedInUI(FirebaseUser firebaseUser) { Log.d(TAG, "Showing signed in UI"); mProfileUi.setVisibility(View.VISIBLE); mProfileUsername.setVisibility(View.VISIBLE); mProfilePhoto.setVisibility(View.VISIBLE); if (firebaseUser.getDisplayName() != null) { mProfileUsername.setText(firebaseUser.getDisplayName()); } if (firebaseUser.getPhotoUrl() != null) { GlideUtil.loadProfileIcon(firebaseUser.getPhotoUrl().toString(), mProfilePhoto); } Map<String, Object> updateValues = new HashMap<>(); updateValues.put("displayName", firebaseUser.getDisplayName() != null ? firebaseUser.getDisplayName() : "Anonymous"); updateValues.put("photoUrl", firebaseUser.getPhotoUrl() != null ? firebaseUser.getPhotoUrl().toString() : null); FirebaseUtil.getPeopleRef().child(firebaseUser.getUid()).child("points") .addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { points.setText(dataSnapshot.getValue().toString() + " Points"); } @Override public void onCancelled(DatabaseError databaseError) { } }); FirebaseUtil.getPeopleRef().child(firebaseUser.getUid()).updateChildren(updateValues, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError firebaseError, DatabaseReference databaseReference) { if (firebaseError != null) { Toast.makeText(ProfileActivity.this, "Couldn't save user data: " + firebaseError.getMessage(), Toast.LENGTH_LONG) .show(); } } }); } private void showSignedOutUI() { Log.d(TAG, "Showing signed out UI"); mProfileUsername.setText(""); mProfileUi.setVisibility(View.GONE); Intent i = new Intent(ProfileActivity.this, LoginRegisterChoose.class); startActivity(i); finish(); } @Override protected void onStart() { super.onStart(); FirebaseUser currentUser = mAuth.getCurrentUser(); if (currentUser != null && !currentUser.isAnonymous()) { dismissProgressDialog(); showSignedInUI(currentUser); } else { showSignedOutUI(); } } @Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { Log.w(TAG, "onConnectionFailed:" + connectionResult); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (Integer.parseInt(android.os.Build.VERSION.SDK) > 5 && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { onBackPressed(); return true; } return super.onKeyDown(keyCode, event); } @Override public void onBackPressed() { startActivity(new Intent(ProfileActivity.this, FeedsActivity.class)); finish(); } }