Advertisement
Guest User

Activity Java

a guest
Oct 9th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.29 KB | None | 0 0
  1.  
  2. public class BlockOrderGCalDaily extends AppCompatActivity {
  3.  
  4.     private TextView mStatusText;
  5.  
  6.     private TextView mResultsText;
  7.  
  8.     /**
  9.      * A Google Calendar API service object used to access the API.
  10.      * Note: Do not confuse this class with API library's model classes, which
  11.      * represent specific data structures.
  12.      */
  13.     com.google.api.services.calendar.Calendar mService;
  14.  
  15.     GoogleAccountCredential credential;
  16.  
  17.     final HttpTransport transport = AndroidHttp.newCompatibleTransport();
  18.     final JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
  19.  
  20.  
  21.     static final int REQUEST_ACCOUNT_PICKER = 1000;
  22.     static final int REQUEST_AUTHORIZATION = 1001;
  23.     static final int REQUEST_GOOGLE_PLAY_SERVICES = 1002;
  24.     private static final String PREF_ACCOUNT_NAME = "accountName";
  25.     private static final String[] SCOPES = { CalendarScopes.CALENDAR_READONLY };
  26.  
  27.     //ANIMATIONS
  28.     // Run Animations for cardviews
  29.     @Override
  30.     protected void onStart () {
  31.         super.onStart();
  32.         if (Build.VERSION.SDK_INT >= 26) {
  33.             StaggeredAnimationGroup group = findViewById(R.id.group);
  34.             group.show();
  35.         }
  36.     }
  37.  
  38.     @Override
  39.     protected void onRestart () {
  40.         super.onRestart();
  41.         if (Build.VERSION.SDK_INT >= 26) {
  42.             StaggeredAnimationGroup group = findViewById(R.id.group);
  43.             group.show();
  44.         }
  45.     }
  46.  
  47.     @Override
  48.     protected void onPause () {
  49.         super.onPause();
  50.         if (Build.VERSION.SDK_INT >= 26) {
  51.             StaggeredAnimationGroup group = findViewById(R.id.group);
  52.             group.hide(true);
  53.         }
  54.     }
  55.  
  56.     @Override
  57.     protected void onStop () {
  58.         super.onStop();
  59.         if (Build.VERSION.SDK_INT >= 26) {
  60.             StaggeredAnimationGroup group = findViewById(R.id.group);
  61.             group.hide(true);
  62.         }
  63.     }
  64.  
  65.     @Override
  66.     protected void onDestroy () {
  67.         super.onDestroy();
  68.         if (Build.VERSION.SDK_INT >= 26) {
  69.             StaggeredAnimationGroup group = findViewById(R.id.group);
  70.             group.hide(true);
  71.         }
  72.     }
  73.  
  74.  
  75.     //ONCREATE
  76.     @Override
  77.     protected void onCreate(Bundle savedInstanceState) {
  78.         super.onCreate(savedInstanceState);
  79.  
  80.         setContentView(R.layout.activity_block_order_gcal_daily);
  81.  
  82.         // Back button code
  83.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarGCAL);
  84.         setSupportActionBar(toolbar);
  85.         getSupportActionBar().setDisplayShowHomeEnabled(true);
  86.         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  87.  
  88.         mResultsText = (TextView)findViewById(R.id.GCalResults);
  89.         mResultsText.setVerticalScrollBarEnabled(true);
  90.         mResultsText.setMovementMethod(new ScrollingMovementMethod());
  91.  
  92.         mStatusText = (TextView)findViewById(R.id.GCalStatus);
  93.         mStatusText.setText("Loading...");
  94.  
  95.  
  96.         // Email button code
  97.         SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  98.  
  99.         if (preferences.getBoolean("switch_preference_misc_emailbutton", true) == true) {
  100.             FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  101.             fab.setOnClickListener(new View.OnClickListener() {
  102.                 @Override
  103.                 public void onClick(View view) {
  104.                     Snackbar.make(view, getString(R.string.EmailButton), Snackbar.LENGTH_LONG);
  105.                     Intent send = new Intent(Intent.ACTION_SENDTO);
  106.                     String uriText = "mailto:" + Uri.encode(getString(R.string.Email)) +
  107.                             "?subject=" + Uri.encode(getString(R.string.EmailTitle)) +
  108.                             "&body=" + Uri.encode(getString(R.string.EmailBody));
  109.                     Uri uri = Uri.parse(uriText);
  110.  
  111.                     send.setData(uri);
  112.                     startActivity(Intent.createChooser(send, "Send mail..."));
  113.                 }
  114.             });
  115.         } else {
  116.             FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  117.             fab.setVisibility(View.GONE);
  118.         }
  119.  
  120.         //EMAIL CODE END -> ONCREATE
  121.  
  122.         // Initialize credentials and service object.
  123.         SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
  124.         credential = GoogleAccountCredential.usingOAuth2(
  125.                 getApplicationContext(), Arrays.asList(SCOPES))
  126.                 .setBackOff(new ExponentialBackOff())
  127.                 .setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
  128.  
  129.         mService = new com.google.api.services.calendar.Calendar.Builder(
  130.                 transport, jsonFactory, credential)
  131.                 .setApplicationName("Clarence Fulton Secondary App")
  132.                 .build();
  133.     }
  134.  
  135.     /**
  136.      * Called whenever this activity is pushed to the foreground, such as after
  137.      * a call to onCreate().
  138.      */
  139.     @Override
  140.     protected void onResume() {
  141.         super.onResume();
  142.         if (isGooglePlayServicesAvailable()) {
  143.             refreshResults();
  144.         } else {
  145.             mStatusText.setText("Google Play Services required: " +
  146.                     "after installing, close and relaunch this app.");
  147.         }
  148.  
  149.         if (Build.VERSION.SDK_INT >= 26) {
  150.             StaggeredAnimationGroup group = findViewById(R.id.group);
  151.             group.show();
  152.         }
  153.     }
  154.  
  155.     /**
  156.      * Called when an activity launched here (specifically, AccountPicker
  157.      * and authorization) exits, giving you the requestCode you started it with,
  158.      * the resultCode it returned, and any additional data from it.
  159.      * @param requestCode code indicating which activity result is incoming.
  160.      * @param resultCode code indicating the result of the incoming
  161.      *     activity result.
  162.      * @param data Intent (containing result data) returned by incoming
  163.      *     activity result.
  164.      */
  165.     @Override
  166.     protected void onActivityResult(
  167.             int requestCode, int resultCode, Intent data) {
  168.         super.onActivityResult(requestCode, resultCode, data);
  169.         switch(requestCode) {
  170.             case REQUEST_GOOGLE_PLAY_SERVICES:
  171.                 if (resultCode == RESULT_OK) {
  172.                     refreshResults();
  173.                 } else {
  174.                     isGooglePlayServicesAvailable();
  175.                 }
  176.                 break;
  177.             case REQUEST_ACCOUNT_PICKER:
  178.                 if (resultCode == RESULT_OK && data != null &&
  179.                         data.getExtras() != null) {
  180.                     String accountName =
  181.                             data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
  182.                     if (accountName != null) {
  183.                         credential.setSelectedAccountName(accountName);
  184.                         SharedPreferences settings =
  185.                                 getPreferences(Context.MODE_PRIVATE);
  186.                         SharedPreferences.Editor editor = settings.edit();
  187.                         editor.putString(PREF_ACCOUNT_NAME, accountName);
  188.                         editor.commit();
  189.                         refreshResults();
  190.                     }
  191.                 } else if (resultCode == RESULT_CANCELED) {
  192.                     mStatusText.setText("Account unspecified.");
  193.                 }
  194.                 break;
  195.             case REQUEST_AUTHORIZATION:
  196.                 if (resultCode == RESULT_OK) {
  197.                     refreshResults();
  198.                 } else {
  199.                     chooseAccount();
  200.                 }
  201.                 break;
  202.         }
  203.  
  204.         super.onActivityResult(requestCode, resultCode, data);
  205.     }
  206.  
  207.     /**
  208.      * Attempt to get a set of data from the Google Calendar API to display. If the
  209.      * email address isn't known yet, then call chooseAccount() method so the
  210.      * user can pick an account.
  211.      */
  212.     private void refreshResults() {
  213.         if (credential.getSelectedAccountName() == null) {
  214.             chooseAccount();
  215.         } else {
  216.             if (isDeviceOnline()) {
  217.                 new ApiAsyncTask(this).execute();
  218.             } else {
  219.                 mStatusText.setText("No network connection available.");
  220.             }
  221.         }
  222.     }
  223.  
  224.     /**
  225.      * Clear any existing Google Calendar API data from the TextView and update
  226.      * the header message; called from background threads and async tasks
  227.      * that need to update the UI (in the UI thread).
  228.      */
  229.     public void clearResultsText() {
  230.         runOnUiThread(new Runnable() {
  231.             @Override
  232.             public void run() {
  233.                 mStatusText.setText("Loading");
  234.                 mResultsText.setText("");
  235.             }
  236.         });
  237.     }
  238.  
  239.     /**
  240.      * Fill the data TextView with the given List of Strings; called from
  241.      * background threads and async tasks that need to update the UI (in the
  242.      * UI thread).
  243.      * @param dataStrings a List of Strings to populate the main TextView with.
  244.      */
  245.     public void updateResultsText(final List<String> dataStrings) {
  246.         runOnUiThread(new Runnable() {
  247.             @Override
  248.             public void run() {
  249.                 if (dataStrings == null) {
  250.                     mStatusText.setText("Error retrieving data!");
  251.                 } else if (dataStrings.size() == 0) {
  252.                     mStatusText.setText("Block Order not found. Ask office to update the ICentre Google calender on the website.");
  253.                 } else {
  254.                     mStatusText.setText("Load Completed");
  255.                     mResultsText.setText(TextUtils.join("\n\n", dataStrings));
  256.                 }
  257.             }
  258.         });
  259.     }
  260.  
  261.     /**
  262.      * Show a status message in the list header TextView; called from background
  263.      * threads and async tasks that need to update the UI (in the UI thread).
  264.      * @param message a String to display in the UI header TextView.
  265.      */
  266.     public void updateStatus(final String message) {
  267.         runOnUiThread(new Runnable() {
  268.             @Override
  269.             public void run() {
  270.                 mStatusText.setText(message);
  271.             }
  272.         });
  273.     }
  274.  
  275.     /**
  276.      * Starts an activity in Google Play Services so the user can pick an
  277.      * account.
  278.      */
  279.     private void chooseAccount() {
  280.         startActivityForResult(
  281.                 credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
  282.     }
  283.  
  284.     /**
  285.      * Checks whether the device currently has a network connection.
  286.      * @return true if the device has a network connection, false otherwise.
  287.      */
  288.     private boolean isDeviceOnline() {
  289.         ConnectivityManager connMgr =
  290.                 (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  291.         NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
  292.         return (networkInfo != null && networkInfo.isConnected());
  293.     }
  294.  
  295.     /**
  296.      * Check that Google Play services APK is installed and up to date. Will
  297.      * launch an error dialog for the user to update Google Play Services if
  298.      * possible.
  299.      * @return true if Google Play Services is available and up to
  300.      *     date on this device; false otherwise.
  301.      */
  302.     private boolean isGooglePlayServicesAvailable() {
  303.         final int connectionStatusCode =
  304.                 GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
  305.         if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
  306.             showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
  307.             return false;
  308.         } else if (connectionStatusCode != ConnectionResult.SUCCESS ) {
  309.             return false;
  310.         }
  311.         return true;
  312.     }
  313.  
  314.     /**
  315.      * Display an error dialog showing that Google Play Services is missing
  316.      * or out of date.
  317.      * @param connectionStatusCode code describing the presence (or lack of)
  318.      *     Google Play Services on this device.
  319.      */
  320.     void showGooglePlayServicesAvailabilityErrorDialog(
  321.             final int connectionStatusCode) {
  322.         runOnUiThread(new Runnable() {
  323.             @Override
  324.             public void run() {
  325.                 Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
  326.                         connectionStatusCode,
  327.                         BlockOrderGCalDaily.this,
  328.                         REQUEST_GOOGLE_PLAY_SERVICES);
  329.                 dialog.show();
  330.             }
  331.         });
  332.     }
  333.  
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement