Android Open Source - UTHPortal-Android-Gradle Settings Screen






From Project

Back to project page UTHPortal-Android-Gradle.

License

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.

Java Source Code

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();
    }
}




Java Source Code List

com.uth.uthportal.AboutScreen.java
com.uth.uthportal.CoursesFragment.java
com.uth.uthportal.DepartmentFragment.java
com.uth.uthportal.FoodFragment.java
com.uth.uthportal.MainScreen.java
com.uth.uthportal.SettingsScreen.java
com.uth.uthportal.adapter.AdapterManager.java
com.uth.uthportal.adapter.AdapterProvider.java
com.uth.uthportal.adapter.ExpandableListAdapter.java
com.uth.uthportal.adapter.SettingsAdapter.java
com.uth.uthportal.adapter.TabsPagerAdapter.java
com.uth.uthportal.buffers.AvailableCoursesParser.java
com.uth.uthportal.buffers.CoursesParser.java
com.uth.uthportal.buffers.FileOperation.java
com.uth.uthportal.buffers.FoodParser.java
com.uth.uthportal.buffers.GeneralAnnParser.java
com.uth.uthportal.buffers.SettingsManager.java
com.uth.uthportal.collections.AnnItem.java
com.uth.uthportal.collections.Announcements.java
com.uth.uthportal.collections.AvailableCourse.java
com.uth.uthportal.collections.CourseInfo.java
com.uth.uthportal.collections.Course.java
com.uth.uthportal.collections.DayMenu.java
com.uth.uthportal.collections.DefaultIntervals.java
com.uth.uthportal.collections.Dish.java
com.uth.uthportal.collections.Food.java
com.uth.uthportal.collections.GeneralAnnouncement.java
com.uth.uthportal.collections.Settings.java
com.uth.uthportal.network.ApiLinks.java
com.uth.uthportal.network.AppRater.java
com.uth.uthportal.network.AsyncJSONDownloader.java
com.uth.uthportal.network.JSONDownloader.java
com.uth.uthportal.service.DataSyncService.java
com.uth.uthportal.util.SystemUiHiderBase.java
com.uth.uthportal.util.SystemUiHiderHoneycomb.java
com.uth.uthportal.util.SystemUiHider.java