Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BlockOrderGCalDaily extends AppCompatActivity {
- private TextView mStatusText;
- private TextView mResultsText;
- /**
- * A Google Calendar API service object used to access the API.
- * Note: Do not confuse this class with API library's model classes, which
- * represent specific data structures.
- */
- com.google.api.services.calendar.Calendar mService;
- GoogleAccountCredential credential;
- final HttpTransport transport = AndroidHttp.newCompatibleTransport();
- final JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
- static final int REQUEST_ACCOUNT_PICKER = 1000;
- static final int REQUEST_AUTHORIZATION = 1001;
- static final int REQUEST_GOOGLE_PLAY_SERVICES = 1002;
- private static final String PREF_ACCOUNT_NAME = "accountName";
- private static final String[] SCOPES = { CalendarScopes.CALENDAR_READONLY };
- //ANIMATIONS
- // Run Animations for cardviews
- @Override
- protected void onStart () {
- super.onStart();
- if (Build.VERSION.SDK_INT >= 26) {
- StaggeredAnimationGroup group = findViewById(R.id.group);
- group.show();
- }
- }
- @Override
- protected void onRestart () {
- super.onRestart();
- if (Build.VERSION.SDK_INT >= 26) {
- StaggeredAnimationGroup group = findViewById(R.id.group);
- group.show();
- }
- }
- @Override
- protected void onPause () {
- super.onPause();
- if (Build.VERSION.SDK_INT >= 26) {
- StaggeredAnimationGroup group = findViewById(R.id.group);
- group.hide(true);
- }
- }
- @Override
- protected void onStop () {
- super.onStop();
- if (Build.VERSION.SDK_INT >= 26) {
- StaggeredAnimationGroup group = findViewById(R.id.group);
- group.hide(true);
- }
- }
- @Override
- protected void onDestroy () {
- super.onDestroy();
- if (Build.VERSION.SDK_INT >= 26) {
- StaggeredAnimationGroup group = findViewById(R.id.group);
- group.hide(true);
- }
- }
- //ONCREATE
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_block_order_gcal_daily);
- // Back button code
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarGCAL);
- setSupportActionBar(toolbar);
- getSupportActionBar().setDisplayShowHomeEnabled(true);
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- mResultsText = (TextView)findViewById(R.id.GCalResults);
- mResultsText.setVerticalScrollBarEnabled(true);
- mResultsText.setMovementMethod(new ScrollingMovementMethod());
- mStatusText = (TextView)findViewById(R.id.GCalStatus);
- mStatusText.setText("Loading...");
- // Email button code
- SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
- if (preferences.getBoolean("switch_preference_misc_emailbutton", true) == true) {
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Snackbar.make(view, getString(R.string.EmailButton), Snackbar.LENGTH_LONG);
- Intent send = new Intent(Intent.ACTION_SENDTO);
- String uriText = "mailto:" + Uri.encode(getString(R.string.Email)) +
- "?subject=" + Uri.encode(getString(R.string.EmailTitle)) +
- "&body=" + Uri.encode(getString(R.string.EmailBody));
- Uri uri = Uri.parse(uriText);
- send.setData(uri);
- startActivity(Intent.createChooser(send, "Send mail..."));
- }
- });
- } else {
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setVisibility(View.GONE);
- }
- //EMAIL CODE END -> ONCREATE
- // Initialize credentials and service object.
- SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
- credential = GoogleAccountCredential.usingOAuth2(
- getApplicationContext(), Arrays.asList(SCOPES))
- .setBackOff(new ExponentialBackOff())
- .setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
- mService = new com.google.api.services.calendar.Calendar.Builder(
- transport, jsonFactory, credential)
- .setApplicationName("Clarence Fulton Secondary App")
- .build();
- }
- /**
- * Called whenever this activity is pushed to the foreground, such as after
- * a call to onCreate().
- */
- @Override
- protected void onResume() {
- super.onResume();
- if (isGooglePlayServicesAvailable()) {
- refreshResults();
- } else {
- mStatusText.setText("Google Play Services required: " +
- "after installing, close and relaunch this app.");
- }
- if (Build.VERSION.SDK_INT >= 26) {
- StaggeredAnimationGroup group = findViewById(R.id.group);
- group.show();
- }
- }
- /**
- * Called when an activity launched here (specifically, AccountPicker
- * and authorization) exits, giving you the requestCode you started it with,
- * the resultCode it returned, and any additional data from it.
- * @param requestCode code indicating which activity result is incoming.
- * @param resultCode code indicating the result of the incoming
- * activity result.
- * @param data Intent (containing result data) returned by incoming
- * activity result.
- */
- @Override
- protected void onActivityResult(
- int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- switch(requestCode) {
- case REQUEST_GOOGLE_PLAY_SERVICES:
- if (resultCode == RESULT_OK) {
- refreshResults();
- } else {
- isGooglePlayServicesAvailable();
- }
- break;
- case REQUEST_ACCOUNT_PICKER:
- if (resultCode == RESULT_OK && data != null &&
- data.getExtras() != null) {
- String accountName =
- data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
- if (accountName != null) {
- credential.setSelectedAccountName(accountName);
- SharedPreferences settings =
- getPreferences(Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = settings.edit();
- editor.putString(PREF_ACCOUNT_NAME, accountName);
- editor.commit();
- refreshResults();
- }
- } else if (resultCode == RESULT_CANCELED) {
- mStatusText.setText("Account unspecified.");
- }
- break;
- case REQUEST_AUTHORIZATION:
- if (resultCode == RESULT_OK) {
- refreshResults();
- } else {
- chooseAccount();
- }
- break;
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
- /**
- * Attempt to get a set of data from the Google Calendar API to display. If the
- * email address isn't known yet, then call chooseAccount() method so the
- * user can pick an account.
- */
- private void refreshResults() {
- if (credential.getSelectedAccountName() == null) {
- chooseAccount();
- } else {
- if (isDeviceOnline()) {
- new ApiAsyncTask(this).execute();
- } else {
- mStatusText.setText("No network connection available.");
- }
- }
- }
- /**
- * Clear any existing Google Calendar API data from the TextView and update
- * the header message; called from background threads and async tasks
- * that need to update the UI (in the UI thread).
- */
- public void clearResultsText() {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- mStatusText.setText("Loading");
- mResultsText.setText("");
- }
- });
- }
- /**
- * Fill the data TextView with the given List of Strings; called from
- * background threads and async tasks that need to update the UI (in the
- * UI thread).
- * @param dataStrings a List of Strings to populate the main TextView with.
- */
- public void updateResultsText(final List<String> dataStrings) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- if (dataStrings == null) {
- mStatusText.setText("Error retrieving data!");
- } else if (dataStrings.size() == 0) {
- mStatusText.setText("Block Order not found. Ask office to update the ICentre Google calender on the website.");
- } else {
- mStatusText.setText("Load Completed");
- mResultsText.setText(TextUtils.join("\n\n", dataStrings));
- }
- }
- });
- }
- /**
- * Show a status message in the list header TextView; called from background
- * threads and async tasks that need to update the UI (in the UI thread).
- * @param message a String to display in the UI header TextView.
- */
- public void updateStatus(final String message) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- mStatusText.setText(message);
- }
- });
- }
- /**
- * Starts an activity in Google Play Services so the user can pick an
- * account.
- */
- private void chooseAccount() {
- startActivityForResult(
- credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
- }
- /**
- * Checks whether the device currently has a network connection.
- * @return true if the device has a network connection, false otherwise.
- */
- private boolean isDeviceOnline() {
- ConnectivityManager connMgr =
- (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
- return (networkInfo != null && networkInfo.isConnected());
- }
- /**
- * Check that Google Play services APK is installed and up to date. Will
- * launch an error dialog for the user to update Google Play Services if
- * possible.
- * @return true if Google Play Services is available and up to
- * date on this device; false otherwise.
- */
- private boolean isGooglePlayServicesAvailable() {
- final int connectionStatusCode =
- GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
- if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
- showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
- return false;
- } else if (connectionStatusCode != ConnectionResult.SUCCESS ) {
- return false;
- }
- return true;
- }
- /**
- * Display an error dialog showing that Google Play Services is missing
- * or out of date.
- * @param connectionStatusCode code describing the presence (or lack of)
- * Google Play Services on this device.
- */
- void showGooglePlayServicesAvailabilityErrorDialog(
- final int connectionStatusCode) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
- connectionStatusCode,
- BlockOrderGCalDaily.this,
- REQUEST_GOOGLE_PLAY_SERVICES);
- dialog.show();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement