1

アプリが Google Plus に接続しているときに接続タイムアウトを設定するために必要な方法を見つけようとしています。しかし、私のためにそれを設定できるものは見つかりません。これが私のコードです。とにかく接続タイムアウトを設定する方法はありますか?

GooglePlusWebService.java

package com.mystuff.WebServices;

import android.app.Activity;
import android.content.IntentSender;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;
import com.google.android.gms.plus.model.people.Person;
import com.pointwest.timetrackermobilelog.Models.ProfileInformation;



public class GooglePlusWebService  {

    // Google client to interact with Google API
    private GoogleApiClient mGoogleApiClient;

    public static String LOG_TAG = GooglePlusWebService.class.getSimpleName();

    private boolean mSignInClicked;
    private boolean mIntentInProgress;
    private ConnectionResult mConnectionResult;
    private static final int RC_SIGN_IN = 0;


    public GooglePlusWebService(Activity activity) {


        mGoogleApiClient = new GoogleApiClient.Builder(activity)
                .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) activity)
                .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) activity)
                .addApi(Plus.API, new Plus.PlusOptions.Builder().build())
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .build();
    }

    public void start() {
//      Log.e(LOG_TAG, "onStart called");
        mGoogleApiClient.connect();
    }

    public void stop() {
//      Log.e(LOG_TAG, "onStop called");
        if (mGoogleApiClient.isConnected()) {
//          Log.e(LOG_TAG, "api connected -> disconnect");
            mGoogleApiClient.disconnect();
        }
    }

    /**
     * Sign-in into google
     * */
    public void signInWithGplus(Activity activity) {
      //Log.e(LOG_TAG, "signInWithGplus called");
        if (!mGoogleApiClient.isConnecting()) {
          //Log.e(LOG_TAG, "api client NOT connecting");
            mSignInClicked = true;
            resolveSignInError(activity);
        } else {
          //Log.e(LOG_TAG, "api client connecting");
        }
    }

    /**
     * Method to resolve any signin errors
     * */
    public void resolveSignInError(Activity activity) {
      //Log.e(LOG_TAG, "resolveSignError called");
        if (mConnectionResult.hasResolution()) {
            try {
              //Log.e(LOG_TAG, "connection result has resolution");
                mIntentInProgress = true;
                mConnectionResult.startResolutionForResult(activity, RC_SIGN_IN);
              //Log.e(LOG_TAG, "connection startResolutionForResult");
            } catch (IntentSender.SendIntentException e) {
               //Log.e(LOG_TAG, "error : ");
                e.printStackTrace();
                mIntentInProgress = false;
                mGoogleApiClient.connect();

            }


        }
    }

    public void connected(Activity activity) {
//       Log.e(LOG_TAG, "onConnected called");
        mSignInClicked = false;
        //Toast.makeText(activity, "User is connected!", Toast.LENGTH_LONG).show();
    }

    public ProfileInformation getProfileInformation() {
       //Log.e(LOG_TAG, "getProfileInformation called");
        ProfileInformation profileInformation = new ProfileInformation();
        try {
            if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                Person currentPerson = Plus.PeopleApi
                        .getCurrentPerson(mGoogleApiClient);
                profileInformation.setPersonName(currentPerson.getDisplayName());
                profileInformation.setPersonPhotoUrl(currentPerson.getImage().getUrl());
                profileInformation.setPersonGooglePlusProfile(currentPerson.getUrl());
                profileInformation.setEmail(Plus.AccountApi.getAccountName(mGoogleApiClient));

//               Log.e(LOG_TAG, "Name: " + profileInformation.getPersonName()
//                        + ", plusProfile: " + profileInformation.getPersonGooglePlusProfile()
//                        + ", email: " + profileInformation.getEmail()
//                        + ", Image: " + profileInformation.getPersonPhotoUrl());
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "Error: ", e );
        }

        return profileInformation;
    }

    /**
     * Sign-out from google
     * */
    public void signOutFromGplus() {
//      Log.e(LOG_TAG, "signOutFromGplus called");
        if (mGoogleApiClient.isConnected()) {
//           Log.e(LOG_TAG, "mGoogleApiClient.isConnected");
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            mGoogleApiClient.connect();
        }
    }



    /**
     * Revoking access from google
     * */
    public void revokeGplusAccess() {
       //Log.e(LOG_TAG, "revokeGplusAccess called");
        if (mGoogleApiClient.isConnected()) {
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
//            Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
//                    .setResultCallback(new ResultCallback<Status>() {
//                        @Override
//                        public void onResult(Status arg0) {
//                           //Log.e(LOG_TAG, "User access revoked!");
//                            mGoogleApiClient.connect();
////                            updateUI(false);
//                        }
//
//                    });
        }
    }

    public void checkActivityResult(int requestCode, int responseCode, int RESULT_OK) {
       //Log.e(LOG_TAG, "onActivityResult called");
        if (requestCode == RC_SIGN_IN) {
            if (responseCode != RESULT_OK) {
                mSignInClicked = false;
            }

            mIntentInProgress = false;

            if (!mGoogleApiClient.isConnecting()) {
                mGoogleApiClient.connect();
            }
        }
    }


    public void connectionFailed(ConnectionResult result, Activity activity) {
//       Log.e(LOG_TAG, "onConnectionFAiled called : " + result.getErrorCode());
        if (!result.hasResolution()) {
           //Log.e(LOG_TAG, "result has no resolution");
            GooglePlayServicesUtil.getErrorDialog (result.getErrorCode(), activity,0).show();
            return;
        }

        if (!mIntentInProgress) {
           //Log.e(LOG_TAG, "mIntent not in progress");
            // Store the ConnectionResult for later usage
            mConnectionResult = result;

            if (mSignInClicked) {
               //Log.e(LOG_TAG, "sign in has clicked");
                // The user has already clicked 'sign-in' so we attempt to
                // resolve all
                // errors until the user is signed in, or they cancel.
                resolveSignInError(activity);
            }
        }
    }
}

そして、それを呼び出すアクティビティ。不要な部分を切り取りました。

MainActivity.java

package com.mystuff.Activities;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.astuetz.PagerSlidingTabStrip;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;


import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



public class MainActivity extends ActionBarActivity implements
        ConnectionCallbacks,
        OnConnectionFailedListener,
        ConnectivityReceiver.OnNetworkAvailableListener,
        MainFragment.SaveButtonListener
{

    public static String LOG_TAG = MainActivity.class.getSimpleName();

    //Application Class
    ApplicationClass applicationClass;

    // Activity to be used for saving tasks into the database;
    private Activity activity;

   // GooglePlus Webservice client to interact with Google API
    private GooglePlusWebService googlePlusWebService;


    //Internet Checker
    private boolean isInternetConnected = false;

    //Share Preferences
    SharedPreferences sharedPreferences;

    //Connection Receiver
    private ConnectivityReceiver connectivityReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
//        Log.e(LOG_TAG, "onCreate called ");

        activity = this;

        setContentView(R.layout.activity_main_material);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        // Initialization of view pager
        viewPager = (ViewPager) findViewById(R.id.pager);

        sharedPreferences = getSharedPreferences("Shared", Context.MODE_PRIVATE);
//        String username = sharedPreferences.getString(SystemConstants.POINTWEST_USERNAME, "");

        if (InternetConnection.isConnected(this)) {
            applicationClass = (ApplicationClass) getApplication();
applicationClass.getTracker(ApplicationClass.TrackerName.APP_TRACKER);
            googlePlusWebService = new GooglePlusWebService(this);
            isInternetConnected = true;

        } 
}
    @Override
    public void onStart() {
        super.onStart();

        if (isInternetConnected) {
            googlePlusWebService.start();

            //Get an Analytics tracker to report app starts and uncaught exceptions etc.
            GoogleAnalytics.getInstance(this).reportActivityStart(this);
        }


    }

    @Override
    public void onStop() {
        Log.e(LOG_TAG, " ## start: onStop called ##");
        super.onStop();
        if (isInternetConnected) {
            googlePlusWebService.stop();

            //Stop the analytics tracking
            GoogleAnalytics.getInstance(this).reportActivityStop(this);
        }



//        Log.e(LOG_TAG, " ## finished: onStop  ##");

    }

    @Override
    public void onDestroy(){
        connectivityReceiver.unbind(activity);

        super.onDestroy();
//        Log.e(LOG_TAG, " ## start: on Destroy called ##");

        tabsPagerAdapter  = new TabsPagerAdapter(
                getSupportFragmentManager(),
                new ArrayList<ProjectTask>(),
                new ArrayList<ProjectTask>());

//        viewPager.setAdapter(tabsPagerAdapter);

//        Log.e(LOG_TAG, " ## finished: on Destroy called ##");
    }


    @Override
    public void onConnected(Bundle bundle) {
        if (isInternetConnected) {
            googlePlusWebService.connected(this);
//            Log.e(LOG_TAG, "onConnected");
        }
        // Get user's information
//        getProfileInformation();
    }

    @Override
    public void onConnectionSuspended(int i) {
        if (isInternetConnected) {
            googlePlusWebService.start();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int responseCode,
                                 Intent intent) {
        if (isInternetConnected) {
            googlePlusWebService.checkActivityResult(requestCode, responseCode, RESULT_OK);
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (isInternetConnected) {
            googlePlusWebService.connectionFailed(connectionResult, this);
        }
    }




}
4

0 に答える 0