Java tutorial
/* * Copyright 2015 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 utkarsh.com.warrantywallet.activities; import android.annotation.TargetApi; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import com.google.firebase.analytics.FirebaseAnalytics; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import com.google.firebase.iid.FirebaseInstanceId; import utkarsh.com.warrantywallet.R; import utkarsh.com.warrantywallet.fragments.MyPostsFragment; import utkarsh.com.warrantywallet.fragments.RecentPostsFragment; import utkarsh.com.warrantywallet.fragments.RssFragmentList; public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener { private FragmentManager mFragmentManager; private FirebaseAuth.AuthStateListener authListener; private FirebaseAuth auth; private FirebaseAnalytics mFirebaseAnalytics; @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); mFragmentManager = getSupportFragmentManager(); auth = FirebaseAuth.getInstance(); mFirebaseAnalytics = FirebaseAnalytics.getInstance(this); //get current user final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); System.out.println("MainActivity.onCreate: " + user.getDisplayName()); System.out.println("MainActivity.onCreate: " + user.getEmail()); System.out.println("MainActivity.onCreate: " + user.getProviderId()); System.out.println("MainActivity.onCreate: " + user.getUid()); System.out.println("MainActivity.onCreate: " + user.getPhotoUrl()); System.out.println("MainActivity.onCreate: " + user.getProviders()); System.out.println("MainActivity.onCreate: " + user.getProviderData().get(0)); System.out.println("MainActivity.onCreate: " + user.getToken(true)); authListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { // FirebaseUser user = firebaseAuth.getCurrentUser(); if (user == null) { // user auth state is changed - user is null // launch login activity startActivity(new Intent(MainActivity.this, LoginActivity.class)); finish(); } } }; mFirebaseAnalytics.setUserProperty("favorite_food", "set your data here"); // set property to save data in analysis logAnalysisEvents(); //set events to be logged FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(MainActivity.this, NewPostActivity.class)); /* Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show();*/ } }); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); } public void signOut() { auth.signOut(); } private void logAnalysisEvents() { // [START image_view_event] Bundle bundle = new Bundle(); bundle.putString(FirebaseAnalytics.Param.ITEM_ID, "1"); bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "name"); bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image"); bundle.putString("full_text", "text"); //custom event mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle); // [END image_view_event] } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_camera) { // Handle the camera action FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.containerView, new RssFragmentList()).commit(); } else if (id == R.id.nav_gallery) { FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.containerView, new MyPostsFragment()).commit(); } else if (id == R.id.nav_slideshow) { FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.containerView, new RecentPostsFragment()).commit(); } else if (id == R.id.profile) { String token = FirebaseInstanceId.getInstance().getToken(); // Log and toast String msg = getString(R.string.msg_token_fmt, token); Log.d("TAG", msg); Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); } else if (id == R.id.nav_share) { } else if (id == R.id.logout) { signOut(); FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user == null) { // user auth state is changed - user is null // launch login activity startActivity(new Intent(MainActivity.this, LoginActivity.class)); finish(); } } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } }