package gabilheri.cloudendpointtest; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.DialogFragment; import android.app.TimePickerDialog; import android.content.Intent; import android.content.IntentSender; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.text.format.DateFormat; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import com.gabilheri.backend.todoApi.model.TodoBean; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.common.SignInButton; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.common.api.Status; import com.google.android.gms.plus.Plus; import com.google.android.gms.plus.model.people.Person; import com.google.api.client.util.DateTime; import java.io.InputStream; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; import it.gmariotti.cardslib.library.internal.Card; import it.gmariotti.cardslib.library.view.CardListView; public class TodoActivity extends Activity implements GoogleApiClient.ConnectionCallbacks , GoogleApiClient.OnConnectionFailedListener { private List<Card> mCardList; private CardListView mCardListView; private List<TodoBean> mTaskList; private EditText mTitleView, mTodoView, mDueAtView; private static final int RC_SIGN_IN = 0; private static final String LOG_TAG = "Cloud Sender"; private static final int PROFILE_PIC_SIZE = 400; private static GoogleApiClient mGoogleServices; private boolean mIntentInProgress; private boolean mSignInClicked; private ConnectionResult mConnectionResult; private SignInButton mSignIn; private Button btnSignOut, btnRevokeAccess; private ImageView imageProfilePic; private TextView txtName, txtEmail; private LinearLayout mProfileLayout; private String personEmail; public static Date dueAtDate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_todo); mCardListView = (CardListView) findViewById(R.id.todoList); mSignIn = (SignInButton) findViewById(R.id.signInButton); mTitleView = (EditText) findViewById(R.id.title); mTodoView = (EditText) findViewById(R.id.message); mDueAtView = (EditText) findViewById(R.id.dueAt); mGoogleServices = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this).addApi(Plus.API) .addScope(Plus.SCOPE_PLUS_LOGIN).build(); } public void createTodo(View view) { Runnable mRun = new Runnable() { @Override public void run() { SenderCloud mSender = new SenderCloud(personEmail, getApplicationContext()); mSender.pushToRemote(mTitleView.getText().toString(), mTodoView.getText().toString(), new DateTime(getDateFromString(mDueAtView.getText().toString()))); } }; new Thread(mRun).start(); } private Date getDateFromString(String dateString) { //SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a"); SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); try { return formatter.parse(dateString); } catch (ParseException ex) { ex.printStackTrace(); return null; } } public void signIn(View view) { signInWithGplus(); } public void showDatePickerDialog(View v) { DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getFragmentManager(), "datePicker"); } @Override protected void onStart() { super.onStart(); mGoogleServices.connect(); } @Override protected void onStop() { super.onStop(); if(mGoogleServices.isConnected()) { mGoogleServices.disconnect(); } } private void updateUI(boolean isSignedIn) { if(isSignedIn) { mSignIn.setVisibility(View.GONE); } else { mSignIn.setVisibility(View.VISIBLE); } } private void getProfileInformation() { try { if(Plus.PeopleApi.getCurrentPerson(mGoogleServices) != null) { Person mCurrentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleServices); String personName = mCurrentPerson.getDisplayName(); //String personPhotoUrl = mCurrentPerson.getImage().getUrl(); String PersonGooglePlusProfile = mCurrentPerson.getUrl(); personEmail = Plus.AccountApi.getAccountName(mGoogleServices); //txtName.setText(personName); //txtEmail.setText(personEmail); // by default the profile url gives 50x50 px image only // we can replace the value with whatever dimension we 2want by // replacing sz=X //personPhotoUrl = personPhotoUrl.substring(0, personPhotoUrl.length() -2) + PROFILE_PIC_SIZE; //new LoadProfileImage(imageProfilePic).execute(personPhotoUrl); /* Runnable mRun = new Runnable() { @Override public void run() { SenderCloud sender = new SenderCloud(personEmail); sender.pushToRemote(); //sender.pullFromRemote(); } }; new Thread(mRun).start(); */ } else { Toast.makeText(getApplicationContext(), "Person information is null", Toast.LENGTH_LONG).show(); } } catch (Exception ex) { ex.printStackTrace(); } } @Override public void onConnected(Bundle bundle) { mSignInClicked = false; Toast.makeText(this, "User is connected!", Toast.LENGTH_SHORT).show(); // get user information. getProfileInformation(); updateUI(true); } @Override public void onConnectionSuspended(int i) { mGoogleServices.connect(); updateUI(false); } @Override protected void onDestroy() { super.onDestroy(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.todo, 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(); if (id == R.id.action_settings) { return true; } else if(id == R.id.action_add) { } return super.onOptionsItemSelected(item); } /** * Sign In with GPLUS */ private void signInWithGplus() { if(!mGoogleServices.isConnecting()) { mSignInClicked = true; resolveSignInError(); } } /** * Sign Out with GPLUS */ private void signOutFromGplus() { if(mGoogleServices.isConnected()) { Plus.AccountApi.clearDefaultAccount(mGoogleServices); mGoogleServices.disconnect(); mGoogleServices.connect(); updateUI(false); } } /** * Revoce access from Gplus */ private void revokeGplusAccess() { if(mGoogleServices.isConnected()) { Plus.AccountApi.clearDefaultAccount(mGoogleServices); Plus.AccountApi.revokeAccessAndDisconnect(mGoogleServices) .setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status status) { Log.i(LOG_TAG, "User access revoked"); mGoogleServices.connect(); updateUI(false); } }); } } /** * * @param connectionResult */ @Override public void onConnectionFailed(ConnectionResult connectionResult) { if(!connectionResult.hasResolution()) { GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show(); return; } if(!mIntentInProgress) { // Store the result for later usage mConnectionResult = connectionResult; if(mSignInClicked) { // The user has already clicked sign-in so we attempt to resolve all errors // until the user is signed in, or cancel. resolveSignInError(); } } } private void resolveSignInError() { if(mConnectionResult.hasResolution()) { try { mIntentInProgress = true; mConnectionResult.startResolutionForResult(this, RC_SIGN_IN); } catch (IntentSender.SendIntentException ex) { mIntentInProgress = false; mGoogleServices.connect(); } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == RC_SIGN_IN) { if(resultCode != RESULT_OK) { mSignInClicked = false; } mIntentInProgress = false; if(!mGoogleServices.isConnecting()) { mGoogleServices.connect(); } } } /** * Background Async task to load user profile picture from url * */ private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> { ImageView bmImage; public LoadProfileImage(ImageView bmImage) { this.bmImage = bmImage; } protected Bitmap doInBackground(String... urls) { String urldisplay = urls[0]; Bitmap mIcon11 = null; try { InputStream in = new java.net.URL(urldisplay).openStream(); mIcon11 = BitmapFactory.decodeStream(in); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } return mIcon11; } protected void onPostExecute(Bitmap result) { bmImage.setImageBitmap(result); } } public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); // Create a new instance of DatePickerDialog and return it return new DatePickerDialog(getActivity(), this, year, month, day); } @Override public void onDateSet(DatePicker view, int year, int month, int day) { // Do something with the date chosen by the user //dueAtDate = new DateTime(view.getYear(), view.getMonth(), view.getDayOfMonth()); } } public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current time as the default values for the picker final Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); // Create a new instance of TimePickerDialog and return it return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity())); } @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // Do something with the time chosen by the user } } }