Back to project page UTHPortal-Android-Gradle.
The source code is released under:
MIT License
If you think the Android project UTHPortal-Android-Gradle listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.uth.uthportal; //from www .j a va 2s .c o m import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutionException; import com.uth.uthportal.adapter.SettingsAdapter; import com.uth.uthportal.buffers.AvailableCoursesParser; import com.uth.uthportal.buffers.SettingsManager; import com.uth.uthportal.collections.AvailableCourse; import com.uth.uthportal.collections.Settings; import com.uth.uthportal.network.ApiLinks; import com.uth.uthportal.network.AsyncJSONDownloader; import com.uth.uthportal.service.DataSyncService; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.CheckBox; import android.widget.ListView; import android.widget.SeekBar; import android.widget.TextView; import android.widget.Toast; public class SettingsScreen extends Activity{ Settings settings; SettingsAdapter settingsAdapter = null; ListView lView; CheckBox notificateCheck; CheckBox autoRefreshCheck; TextView refreshTextView; SeekBar intervalBar; //map indexes to values final int intervalValues[] = {2,5,10,20,30,60,120,360,720,1440}; //get caption text String refreshCaption; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); refreshCaption = getResources().getString(R.string.refreshText); settings = SettingsManager.loadSettings(this); //load settings //get controls lView = (ListView)findViewById(R.id.listAvaibleCourses); autoRefreshCheck = (CheckBox)findViewById(R.id.autoRefreshCheck); notificateCheck = (CheckBox)findViewById(R.id.notificateCheck); refreshTextView = (TextView) findViewById(R.id.refreshText); intervalBar = (SeekBar) findViewById(R.id.intervalBar); //------------- // Listeners! intervalBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //Change caption TextView with appropriate text refreshTextView.setText( String.format( "%s: %s", refreshCaption, valToString(intervalValues[progress]) ) ); settings.refreshInterval = intervalValues[progress]; //change the actual setting } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); autoRefreshCheck.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { boolean isChecked =((CheckBox)v).isChecked(); settings.autoRefresh = isChecked; //disable-enable the seek bar intervalBar.setEnabled(isChecked); } }); notificateCheck.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { settings.notify = ((CheckBox)v).isChecked(); } }); //---------------------------------------------------------------- autoRefreshCheck.setChecked(settings.autoRefresh); intervalBar.setEnabled(autoRefreshCheck.isChecked()); notificateCheck.setChecked(settings.notify); //search in intervalValues array for the index that corresponds to settings value. intervalBar.setProgress(Arrays.binarySearch(intervalValues, settings.refreshInterval)); List<AvailableCourse> availableCourseList = getAvailableCourses(); if (availableCourseList == null){ // couldn't get available courses Toast.makeText(this, "??? ???????? ???????? ??? ?????????!",Toast.LENGTH_LONG).show(); return; } //Find out if we want notifications from the newly downloaded courses list. for (String str : settings.courses){ for (AvailableCourse course : availableCourseList) { if (course.code.equals(str)) { course.getAnnouncements = true; } } } settingsAdapter = new SettingsAdapter(this,R.layout.list_settings_item, availableCourseList); lView.setAdapter(settingsAdapter); } private String valToString(int mins){ switch(mins){ case 2: return "2 ?????"; case 5: return "5 ?????"; case 10: return "10 ?????"; case 20: return "20 ?????"; case 30: return "30 ?????"; case 60: return "1 ????"; case 120: return "2 ?????"; case 360: return "6 ?????"; case 720: return "12 ?????"; case 1440: return "1 ?????"; default: return "None"; } } private List<AvailableCourse> getAvailableCourses(){ List<AvailableCourse> availableCourses = new ArrayList<AvailableCourse>(); String jBuffer; AsyncJSONDownloader asyncJSONDownloader = new AsyncJSONDownloader(); try { jBuffer = asyncJSONDownloader.execute(ApiLinks.getAvailableCoursesLink()).get(); } catch (InterruptedException ex) { Log.e("Downloading courses failed.", ex.getMessage()); return availableCourses; } catch (ExecutionException ex){ Log.e("Downloading courses failed.", ex.getMessage()); return availableCourses; } if (jBuffer == null || jBuffer.isEmpty()){ Log.e("Downloading courses failed.", "Exception not caught"); return null; } AvailableCoursesParser availableCoursesParser = new AvailableCoursesParser(); availableCourses = availableCoursesParser.parseAvailableCourses(jBuffer); if (availableCourses == null || !availableCoursesParser.wasSuccessful){ Log.e("Failed to parse available courses", availableCoursesParser.errorMessage); return null; } return availableCourses; } @Override public void onDestroy() { if (settingsAdapter != null) { settings.courses = settingsAdapter.getCheckedCourses(); } SettingsManager.saveSettings(settings, this); //save settings before we leave //Force courses update Intent msgIntent = new Intent(this,DataSyncService.class); startService(msgIntent); super.onDestroy(); } }