Java tutorial
/* * Copyright (C) 2014 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.crackingbits.bummer; import android.content.Context; import android.content.Intent; import android.media.MediaPlayer; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.content.LocalBroadcastManager; import android.widget.Toast; import com.crackingbits.bummer.fragments.FreshdeskDetailFragment; import com.crackingbits.bummer.fragments.JenkinsDetailFragment; import com.crackingbits.bummer.fragments.MainFragment; import com.crackingbits.bummer.fragments.SettingsFragment; import com.crackingbits.bummer.model.Job; import com.crackingbits.bummer.model.Ticket; import com.crackingbits.bummer.task.FreshdeskTask; import com.crackingbits.bummer.task.JenkinsTask; import com.crackingbits.bummer.task.OkHttpTask; import com.squareup.otto.Subscribe; import java.util.ArrayList; import java.util.HashMap; import butterknife.ButterKnife; /* * MainActivity class that loads MainFragment */ public class MainActivity extends FragmentActivity { public long lastInteraction; private Config config; private boolean isFirstFragment = true; MediaPlayer hackSound; MediaPlayer aplauseSound; public ArrayList<Job> sortedJobs; public ArrayList<Ticket> sortedTickets; private JenkinsTask jobListTask; private HashMap<String, Job> jobsHashMap = new HashMap<String, Job>(); private FreshdeskTask freshdeskTask; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EventBus.getInstance().register(this); setContentView(R.layout.activity_main); ButterKnife.bind(this); config = getConfig(); hackSound = MediaPlayer.create(this, R.raw.hack); aplauseSound = MediaPlayer.create(this, R.raw.claps); getFreshDesk(); getJenkinsJobs(); fetchData(); if (config.getIsFirstLaunch()) { showMainFragment(); showSettingsFragment(); config.setIsFirstLaunch(false); } else { showMainFragment(); } } @Override protected void onDestroy() { EventBus.getInstance().unregister(this); super.onDestroy(); } public void showMainFragment() { MainFragment mainFragment = new MainFragment(); showFragment(mainFragment); } public void showFreshdeskDetailFragment(Ticket ticket) { FreshdeskDetailFragment freshdeskDetailFragment = new FreshdeskDetailFragment(); freshdeskDetailFragment.ticket = ticket; showFragment(freshdeskDetailFragment); } public void showJenkinsDetailFragment(Job job) { JenkinsDetailFragment jenkinsDetailFragment = new JenkinsDetailFragment(); jenkinsDetailFragment.job = job; showFragment(jenkinsDetailFragment); } public void showSettingsFragment() { SettingsFragment settingsFragment = new SettingsFragment(); showFragment(settingsFragment); } public void showFragment(Fragment fragment) { FragmentManager fragmentManager = getSupportFragmentManager(); if (isFirstFragment) { isFirstFragment = false; fragmentManager.beginTransaction().replace(R.id.contentFrame, fragment).commit(); } else { fragmentManager.beginTransaction().replace(R.id.contentFrame, fragment).addToBackStack(null).commit(); } } public boolean stopScrolling() { long time = System.currentTimeMillis() - lastInteraction; if (time > 5000) { return true; } return false; } @Override public void onUserInteraction() { lastInteraction = System.currentTimeMillis(); Intent intent = new Intent("userInteraction"); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); EventBus.getInstance().post(Constants.USER_INTERACTION); } public Config getConfig() { if (config == null) { config = new Config(this); } return config; } public void playSound(String sound) { if (sound.equals(Constants.HACK)) hackSound.start(); if (sound.equals(Constants.APLAUSE)) aplauseSound.start(); } public void getFreshDesk() { freshdeskTask = new FreshdeskTask(this); freshdeskTask.execute(); } public void getJenkinsJobs() { jobListTask = new JenkinsTask(this); jobListTask.execute(); } public void createJobHashMap(ArrayList<Job> list) { jobsHashMap.clear(); for (Job job : list) { jobsHashMap.put(job.name, job); } } public void fetchData() { fetchJenkins(); fetchFreshdesk(); } public void fetchJenkins() { final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { if (isNetworkAvailable()) { getJenkinsJobs(); } fetchJenkins(); } }, 20000); } public void fetchFreshdesk() { final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { if (isNetworkAvailable()) { getFreshDesk(); } fetchFreshdesk(); } }, 5 * 60 * 1000); } public void checkChangesInJobs(ArrayList<Job> list) { for (Job job : list) { Job j = jobsHashMap.get(job.name); if (j != null) { if (!j.color.equals(job.color)) { if (job.color.equals("red")) { playSound(Constants.HACK); showInfoToast(job.name + " failed."); } if (job.color.equals("blue")) { playSound(Constants.APLAUSE); showInfoToast(job.name + " backed to normal."); } } } } } public boolean isNetworkAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } public void showInfoToast(String text) { Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG).show(); } @Subscribe public void onAsyncTaskResult(OkHttpTask task) { if (task.getClass() == JenkinsTask.class) { JenkinsTask t = (JenkinsTask) task; sortedJobs = Job.sortJobs(t.list); checkChangesInJobs(t.list); createJobHashMap(t.list); EventBus.getInstance().post(Constants.REFRESH); } if (task.getClass() == FreshdeskTask.class) { FreshdeskTask ft = (FreshdeskTask) task; if (config.getSortByPriority()) sortedTickets = Ticket.sortJobsByPriority(ft.ticketlist); else sortedTickets = ft.ticketlist; EventBus.getInstance().post(Constants.REFRESH); } } }