Java tutorial
/* * Copyright (C) 2013 The Android Open Source Project * * 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 com.example.android.oauthTesting_slidingTabBasic; import com.example.android.common.logger.Log; import com.example.android.common.view.SlidingTabLayout; import com.facebook.CallbackManager; import com.facebook.FacebookCallback; import com.facebook.FacebookException; import com.facebook.FacebookSdk; import com.facebook.login.LoginResult; import com.facebook.login.widget.LoginButton; import com.google.android.gms.auth.api.Auth; import com.google.android.gms.auth.api.signin.GoogleSignInAccount; import com.google.android.gms.auth.api.signin.GoogleSignInOptions; import com.google.android.gms.auth.api.signin.GoogleSignInResult; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.SignInButton; import com.google.android.gms.common.api.GoogleApiClient; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.telephony.PhoneNumberFormattingTextWatcher; import android.telephony.PhoneNumberUtils; 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.support.v4.app.FragmentPagerAdapter; import android.content.Context; import android.support.v4.app.FragmentManager; /** * A basic sample which shows how to use {@link com.example.android.common.view.SlidingTabLayout} * to display a custom {@link ViewPager} title strip which gives continuous feedback to the user * when scrolling. */ public class SlidingTabsBasicFragment extends Fragment implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener { static final String LOG_TAG = "SlidingTabsBasicFragment"; private static final int RC_SIGN_IN = 9001; /** * A custom {@link ViewPager} title strip which looks much like Tabs present in Android v4.0 and * above, but is designed to give continuous feedback to the user when scrolling. */ private SlidingTabLayout mSlidingTabLayout; /** * A {@link ViewPager} which will be used in conjunction with the {@link SlidingTabLayout} above. */ private ViewPager mViewPager; //-----My FACEBOOK OAUTH TRIAL----// private LoginButton loginButton; private CallbackManager callbackManager; private GoogleApiClient mGoogleApiClient; /** * Inflates the {@link View} which will be displayed by this {@link Fragment}, from the app's * resources. */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { FacebookSdk.sdkInitialize(getActivity().getApplicationContext()); return inflater.inflate(R.layout.fragment_sample, container, false); } // BEGIN_INCLUDE (fragment_onviewcreated) /** * This is called after the {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} has finished. * Here we can pick out the {@link View}s we need to configure from the content view. * * We set the {@link ViewPager}'s adapter to be an instance of {@link SamplePagerAdapter}. The * {@link SlidingTabLayout} is then given the {@link ViewPager} so that it can populate itself. * * @param view View created in {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} */ @Override public void onViewCreated(View view, Bundle savedInstanceState) { // BEGIN_INCLUDE (setup_viewpager) // Get the ViewPager and set it's PagerAdapter so that it can display items mViewPager = (ViewPager) view.findViewById(R.id.viewpager); //mViewPager.setAdapter(new SimpleViewPagerAdapter(this.getChildFragmentManager())); mViewPager.setAdapter(new SimpleViewPagerAdapter(this.getChildFragmentManager())); // END_INCLUDE (setup_viewpager) // BEGIN_INCLUDE (setup_slidingtablayout) // Give the SlidingTabLayout the ViewPager, this must be done AFTER the ViewPager has had // it's PagerAdapter set. mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs); mSlidingTabLayout.setViewPager(mViewPager); final EditText recipient_phone = (EditText) view.findViewById(R.id.recipient_phone); recipient_phone.addTextChangedListener(new PhoneNumberFormattingTextWatcher()); final Button send_sms = (Button) view.findViewById(R.id.send_sms); send_sms.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String number = recipient_phone.getText().toString(); String s = PhoneNumberUtils.stripSeparators(recipient_phone.getText().toString()); String q = PhoneNumberUtils.formatNumber(s, "US"); String r = q; } // END_INCLUDE (setup_slidingtablayout) }); //----FACEBOOK OAUTH TRIAL STUFF----// callbackManager = CallbackManager.Factory.create(); loginButton = (LoginButton) view.findViewById(R.id.login_button); loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { int a = 5; int b = a; } @Override public void onCancel() { } @Override public void onError(FacebookException e) { } }); // END_INCLUDE (fragment_onviewcreated) GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail().build(); mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) .enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */) .addApi(Auth.GOOGLE_SIGN_IN_API, gso).build(); SignInButton signInButton = (SignInButton) view.findViewById(R.id.sign_in_button); signInButton.setOnClickListener(this); } private void signIn() { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.sign_in_button: signIn(); break; // ... } } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); handleSignInResult(result); } callbackManager.onActivityResult(requestCode, resultCode, data); } private void handleSignInResult(GoogleSignInResult result) { //Log.d(TAG, "handleSignInResult:" + result.isSuccess()); if (result.isSuccess()) { // Signed in successfully, show authenticated UI. GoogleSignInAccount acct = result.getSignInAccount(); String displayName = acct.getDisplayName(); acct.getIdToken(); //mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName())); // updateUI(true); } else { // Signed out, show unauthenticated UI. //updateUI(false); } } /** * The {@link android.support.v4.view.PagerAdapter} used to display pages in this sample. * The individual pages are simple and just display two lines of text. The important section of * this class is the {@link #getPageTitle(int)} method which controls what is displayed in the * {@link SlidingTabLayout}. */ class SamplePagerAdapter extends PagerAdapter { /** * @return the number of pages to display */ @Override public int getCount() { return 5; } /** * @return true if the value returned from {@link #instantiateItem(ViewGroup, int)} is the * same object as the {@link View} added to the {@link ViewPager}. */ @Override public boolean isViewFromObject(View view, Object o) { return o == view; } // BEGIN_INCLUDE (pageradapter_getpagetitle) /** * Return the title of the item at {@code position}. This is important as what this method * returns is what is displayed in the {@link SlidingTabLayout}. * <p> * Here we construct one using the position value, but for real application the title should * refer to the item's contents. */ @Override public CharSequence getPageTitle(int position) { return "Item " + (position + 1); } // END_INCLUDE (pageradapter_getpagetitle) /** * Instantiate the {@link View} which should be displayed at {@code position}. Here we * inflate a layout from the apps resources and then change the text view to signify the position. */ @Override public Object instantiateItem(ViewGroup container, int position) { // Inflate a new layout from our resources View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item, container, false); // Add the newly created View to the ViewPager container.addView(view); // Retrieve a TextView from the inflated View, and update it's text TextView title = (TextView) view.findViewById(R.id.item_title); title.setText(String.valueOf(position + 1)); Log.i(LOG_TAG, "instantiateItem() [position: " + position + "]"); // Return the View return view; } /** * Destroy the item from the {@link ViewPager}. In our case this is simply removing the * {@link View}. */ @Override public void destroyItem(ViewGroup container, int position, Object object) { // container.removeView((View) object); Log.i(LOG_TAG, "destroyItem() [position: " + position + "]"); } } public class SimpleViewPagerAdapter extends FragmentPagerAdapter { final int PAGE_COUNT = 5; // Tab Titles private String tabtitles[] = new String[] { "Tab1", "Tab2", "Tab3", "Tab4", "Tab5" }; Context context; public SimpleViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return PAGE_COUNT; } @Override public Fragment getItem(int position) { switch (position) { // Open FragmentTab1.java case 0: SampleFragment1 fragmenttab1 = new SampleFragment1(); return fragmenttab1; // Open FragmentTab2.java case 1: SampleFragment2 fragmenttab2 = new SampleFragment2(); return fragmenttab2; case 2: SampleFragment1 fragmenttab3 = new SampleFragment1(); return fragmenttab3; // Open FragmentTab2.java case 3: SampleFragment2 fragmenttab4 = new SampleFragment2(); return fragmenttab4; case 4: SampleFragment2 fragmenttab5 = new SampleFragment2(); return fragmenttab5; // Open FragmentTab3.java /* case 2: FragmentTab3 fragmenttab3 = new FragmentTab3(); return fragmenttab3;*/ } return null; } @Override public CharSequence getPageTitle(int position) { return tabtitles[position]; } } }