/* * Copyright 2012 Tallinn University Centre for Educational Technology * * 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 ee.htk.dippler.app; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.content.Intent; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import ee.htk.dippler.app.ArrayAdapters.CourseArrayAdapter; import ee.htk.dippler.app.entities.Course; import ee.htk.dippler.app.service.Connector; import ee.htk.dippler.app.service.ListingManager; public class CoursesActivity extends ListActivity implements OnClickListener { private static final int SETTINGS_MENU_ITEM = Menu.FIRST; private static final int PREFS_ACTIVITY = 1; private Connector service_connector = null; private List courses = new ArrayList(); private CourseArrayAdapter mAdapter; private ListingManager listingManager; private View progress; private View load_failed; private View reload_btn; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); service_connector = new Connector(getApplicationContext()); setContentView(R.layout.course_listing); progress = findViewById(R.id.progress_wrapper); load_failed = findViewById(R.id.load_failed); reload_btn = findViewById(R.id.try_again_btn); reload_btn.setOnClickListener(this); //Check if application is configure, if no username or password preference found, launch prefs activity SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); if ( !prefs.contains("dippler-username") && !prefs.contains("dippler-password") ) { launchPreferencesActivity(); } else { new ConnectService().execute(); } mAdapter = new CourseArrayAdapter(getApplicationContext(), R.layout.list_course, courses); setListAdapter(mAdapter); getListView().setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) { Intent single_course = new Intent(getApplicationContext(), CourseSingleActivity.class); single_course.putExtra("dippler-course", courses.get(position) ); single_course.putExtra("dippler-session", service_connector.getSession()); startActivity(single_course); } }); } private class fetchCourses extends AsyncTask> { @Override protected List doInBackground(String... params) { listingManager = new ListingManager(getApplicationContext(), service_connector.getSessionId()); List courses = listingManager.getCourses(); return courses; } @Override protected void onPostExecute(List courses_in) { super.onPostExecute(courses_in); courses.addAll(courses_in); mAdapter.notifyDataSetChanged(); progress.setVisibility(View.GONE); } } class ConnectService extends AsyncTask { @Override protected String doInBackground(String... params) { return service_connector.connect(); } @Override protected void onPostExecute(String result) { super.onPostExecute(result); Log.i("dippler-app", "Connect result "+result); if (result != null) { //Finally start fetching new fetchCourses().execute(); } else { load_failed.setVisibility(View.VISIBLE); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, SETTINGS_MENU_ITEM, 0, R.string.settings_menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { case SETTINGS_MENU_ITEM: launchPreferencesActivity(); return true; } return super.onOptionsItemSelected(item); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if ( requestCode == PREFS_ACTIVITY ) { progress.setVisibility(View.VISIBLE); courses.clear(); mAdapter.notifyDataSetChanged(); new ConnectService().execute(); } } private void launchPreferencesActivity() { Intent settings_intent = new Intent(this, PreferencesActivity.class); startActivityForResult(settings_intent, PREFS_ACTIVITY); } @Override public void onClick(View v) { if ( v.getId() == R.id.try_again_btn ) { load_failed.setVisibility(View.GONE); progress.setVisibility(View.VISIBLE); new ConnectService().execute(); } } }