~lsgadmins/lsgapp/trunk

« back to all changes in this revision

Viewing changes to src/com/lsg/app/VPlan.java

  • Committer: maximilian
  • Date: 2012-06-20 05:31:53 UTC
  • Revision ID: maximilian-20120620053153-kvosxm6261rye15y
*Started implementing viewpager titles
*fixed timetable combination with vplan

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import android.database.Cursor;
19
19
import android.database.sqlite.SQLiteDatabase;
20
20
import android.database.sqlite.SQLiteStatement;
 
21
import android.graphics.Point;
21
22
import android.os.AsyncTask;
22
23
import android.os.Bundle;
23
24
import android.os.Parcelable;
29
30
import android.util.Log;
30
31
import android.view.ContextMenu;
31
32
import android.view.ContextMenu.ContextMenuInfo;
 
33
import android.view.Display;
32
34
import android.view.LayoutInflater;
33
35
import android.view.Menu;
34
36
import android.view.MenuInflater;
35
37
import android.view.MenuItem;
36
38
import android.view.View;
 
39
import android.view.View.OnClickListener;
37
40
import android.view.ViewGroup;
38
41
import android.webkit.WebView;
39
42
import android.widget.CursorAdapter;
51
54
                private final SQLiteDatabase myDB;
52
55
                public Cursor cursor_all;
53
56
                public Cursor cursor_mine;
 
57
                public Cursor cursor_teachers;
54
58
                private VPlan.VertretungAdapter vadapter_all;
55
59
                private VPlan.VertretungAdapter vadapter_mine;
 
60
                private VPlan.VertretungAdapter vadapter_teachers;
56
61
                private String exclude_cond;
57
62
                private String include_cond;
58
63
                private VPlan act;
90
95
                        
91
96
                        SQLiteStatement num_rows = myDB.compileStatement("SELECT COUNT(*) FROM " + Functions.DB_VPLAN_TABLE);
92
97
                        long count = num_rows.simpleQueryForLong();
93
 
                        if(count == 0)
 
98
                        SQLiteStatement num_rows_2 = myDB.compileStatement("SELECT COUNT(*) FROM " + Functions.DB_VPLAN_TEACHER);
 
99
                        long count2 = num_rows_2.simpleQueryForLong();
 
100
                        if(count == 0 || count2 == 0)
94
101
                                act.updateVP();
95
102
                        num_rows.close();
96
 
                        vadapter_all = new VPlan.VertretungAdapter(context, cursor_all);
97
 
                        vadapter_mine = new VPlan.VertretungAdapter(context, cursor_mine);
 
103
                        vadapter_all = new VPlan.VertretungAdapter(context, cursor_all, false);
 
104
                        vadapter_mine = new VPlan.VertretungAdapter(context, cursor_mine, false);
 
105
                        vadapter_teachers = new VPlan.VertretungAdapter(context, cursor_teachers, true);
98
106
                        updateCursor();
99
107
                        }
100
108
                
101
 
        /*      @Override
102
 
                public String getTitle( int position )
103
 
                {
104
 
                        super.get
105
 
                        return titles[ position ];
106
 
                        }*/
107
 
                
108
109
                @Override
109
110
                public int getCount() {
110
 
                        return 2;
 
111
                        return 3;
111
112
                        }
112
113
                
113
114
                @Override
126
127
                                        lv.setAdapter(vadapter_mine);
127
128
                                if(position == 1)
128
129
                                        lv.setAdapter(vadapter_all);
 
130
                                if(position == 2)
 
131
                                        lv.setAdapter(vadapter_teachers);
129
132
                                Functions.styleListView(lv, context);
130
133
                                act.registerForContextMenu(lv);
131
134
                                lv.setEmptyView(lay.findViewById(R.id.list_view_empty));
133
136
                                        ((TextView) lay.findViewById(R.id.list_view_empty)).setText(R.string.vplan_mine_empty);
134
137
                                if(position == 1)
135
138
                                        ((TextView) lay.findViewById(R.id.list_view_empty)).setText(R.string.vplan_empty);
 
139
                                if(position == 2)
 
140
                                        ((TextView) lay.findViewById(R.id.list_view_empty)).setText(R.string.vplan_empty);
136
141
                                ((ViewPager)pager).addView(lay, 0);
137
142
                                return lay;
138
143
                                }
192
197
                        cursor_all = myDB.query(Functions.DB_VPLAN_TABLE, new String [] {Functions.DB_ROWID, Functions.DB_KLASSE, Functions.DB_ART, Functions.DB_STUNDE,
193
198
                                        Functions.DB_LEHRER, Functions.DB_FACH, Functions.DB_VERTRETUNGSTEXT, Functions.DB_VERTRETER, Functions.DB_RAUM,
194
199
                                        Functions.DB_KLASSENSTUFE, Functions.DB_DATE, Functions.DB_LENGTH}, all_cond, where_conds, null, null, null);
 
200
                        cursor_teachers = myDB.query(Functions.DB_VPLAN_TEACHER, new String [] {Functions.DB_ROWID, Functions.DB_KLASSE, Functions.DB_ART, Functions.DB_STUNDE,
 
201
                                        Functions.DB_LEHRER, Functions.DB_FACH, Functions.DB_VERTRETUNGSTEXT, Functions.DB_VERTRETER, Functions.DB_RAUM,
 
202
                                        Functions.DB_KLASSENSTUFE, Functions.DB_DATE, Functions.DB_LENGTH}, all_cond, where_conds, null, null, null);;
195
203
                        vadapter_mine.changeCursor(cursor_mine);
196
204
                        vadapter_all.changeCursor(cursor_all);
 
205
                        vadapter_teachers.changeCursor(cursor_teachers);
197
206
                }
198
207
                public void updateWhereCond(String searchText) {
199
208
                        where_conds[1] = "%" + searchText + "%";
246
255
                @Override
247
256
                public void startUpdate( View view ) {}
248
257
        }
249
 
        public static class VertretungAdapter extends CursorAdapter {
 
258
        public class VertretungAdapter extends CursorAdapter {
 
259
                private boolean teacher;
250
260
                class Standard {
251
261
                        public LinearLayout standard;
252
262
                        public TextView date;
258
268
                        public TextView bottom;
259
269
                        public WebView webv;
260
270
                        }
261
 
                public VertretungAdapter(Context context, Cursor c) {
 
271
                public VertretungAdapter(Context context, Cursor c, boolean teacher) {
262
272
                        super(context, c, false);
 
273
                        this.teacher = teacher;
263
274
                        }
264
275
                @Override
265
276
                public View newView(Context context, Cursor cursor, ViewGroup parent) {
266
277
                        LayoutInflater inflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
267
 
                        View rowView = inflater.inflate(R.layout.standard, null, true);
 
278
                        View rowView;
 
279
                        if(teacher)
 
280
                                rowView = inflater.inflate(R.layout.standard, null, true);
 
281
                        else
 
282
                                rowView = inflater.inflate(R.layout.standard, null, true);
268
283
                        Standard holder = new Standard();
269
284
                        holder.standard = (LinearLayout) rowView.findViewById(R.id.standard_rellayout);
270
285
                        holder.date = (TextView) rowView.findViewById(R.id.vertretung_date);
331
346
                                holder.title.setText(klasse + " (" + fach + ")");
332
347
                                String type = cursor.getString(cursor.getColumnIndex(Functions.DB_ART));
333
348
                                holder.type.setText(type);
334
 
                                
335
 
                                Integer lesson = Integer.valueOf(cursor.getString(cursor.getColumnIndex(Functions.DB_STUNDE)));
 
349
                                Integer lesson;
 
350
                                try {
 
351
                                lesson = Integer.valueOf(cursor.getString(cursor.getColumnIndex(Functions.DB_STUNDE)));
 
352
                                } catch(Exception e) {
 
353
                                        //old db style, do act!!!
 
354
                                        lesson = 0;
 
355
                                        VPlan.this.updateVP();
 
356
                                }
336
357
                                String when = lesson.toString();
337
358
                                int i = 0;
338
359
                                int length = cursor.getInt(cursor.getColumnIndex(Functions.DB_LENGTH));
405
426
                                                values.put(Functions.DB_KLASSE, jObject.getString("klasse"));
406
427
                                                values.put(Functions.DB_STUNDE, jObject.getString("stunde"));
407
428
                                                values.put(Functions.DB_VERTRETER, jObject.getString("vertreter"));
 
429
                                                values.put(Functions.DB_RAW_VERTRETER, jObject.getString("rawvertreter"));
408
430
                                                values.put(Functions.DB_LEHRER, jObject.getString("lehrer"));
 
431
                                                values.put(Functions.DB_RAW_LEHRER, jObject.getString("rawlehrer"));
409
432
                                                values.put(Functions.DB_RAUM, jObject.getString("raum"));
410
433
                                                values.put(Functions.DB_ART, jObject.getString("art"));
411
434
                                                values.put(Functions.DB_VERTRETUNGSTEXT, jObject.getString("vertretungstext"));
439
462
                                return new String[] {"unknownerror", context.getString(R.string.unknownerror)};
440
463
                        return new String[] {"success", " "};
441
464
                        }
 
465
                public String[] updateTeachers() {
 
466
                                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
 
467
                                String add = "";
 
468
                                try {
 
469
                                        add = "&" + URLEncoder.encode("date", "UTF-8") + "=" + URLEncoder.encode(prefs.getString("vplan_teacher_date", ""), "UTF-8")
 
470
                                                        + "&" + URLEncoder.encode("time", "UTF-8") + "=" + URLEncoder.encode(prefs.getString("vplan_teacher_time", ""), "UTF-8")
 
471
                                                        + "&" + URLEncoder.encode("type", "UTF-8") + "=" + URLEncoder.encode("teachers", "UTF-8");
 
472
                                        } catch(UnsupportedEncodingException e) { Log.d("encoding", e.getMessage()); }
 
473
                                String get = Functions.getData(Functions.VP_URL, context, true, add);
 
474
                                Log.d("get", get);
 
475
                                if(!get.equals("networkerror") && !get.equals("loginerror") && !get.equals("noact") && !get.equals("rights")) {
 
476
                                        try {
 
477
                                                JSONArray jArray = new JSONArray(get);
 
478
                                                int i = 0;
 
479
                                                SQLiteDatabase myDB = context.openOrCreateDatabase(Functions.DB_NAME, Context.MODE_PRIVATE, null);
 
480
                                                myDB.delete(Functions.DB_VPLAN_TEACHER, null, null); //clear vertretungen
 
481
                                                while(i < jArray.length() - 1) {
 
482
                                                        JSONObject jObject = jArray.getJSONObject(i);
 
483
                                                        ContentValues values = new ContentValues();
 
484
                                                        values.put(Functions.DB_KLASSENSTUFE, jObject.getString("klassenstufe"));
 
485
                                                        values.put(Functions.DB_KLASSE, jObject.getString("klasse"));
 
486
                                                        values.put(Functions.DB_STUNDE, jObject.getString("stunde"));
 
487
                                                        values.put(Functions.DB_VERTRETER, jObject.getString("vertreter"));
 
488
                                                        values.put(Functions.DB_RAW_VERTRETER, jObject.getString("rawvertreter"));
 
489
                                                        values.put(Functions.DB_LEHRER, jObject.getString("lehrer"));
 
490
                                                        values.put(Functions.DB_RAW_LEHRER, jObject.getString("rawlehrer"));
 
491
                                                        values.put(Functions.DB_RAUM, jObject.getString("raum"));
 
492
                                                        values.put(Functions.DB_ART, jObject.getString("art"));
 
493
                                                        values.put(Functions.DB_VERTRETUNGSTEXT, jObject.getString("vertretungstext"));
 
494
                                                        values.put(Functions.DB_FACH, jObject.getString("fach"));
 
495
                                                        values.put(Functions.DB_RAW_FACH, jObject.getString("rawfach"));
 
496
                                                        values.put(Functions.DB_DATE, jObject.getString("date"));
 
497
                                                        values.put(Functions.DB_LENGTH, jObject.getInt("length"));
 
498
                                                        myDB.insert(Functions.DB_VPLAN_TEACHER, null, values);
 
499
                                                        i++;
 
500
                                                        }
 
501
                                                myDB.close();
 
502
                                                JSONObject jObject            = jArray.getJSONObject(i);
 
503
                                                String date                   = jObject.getString("date");
 
504
                                                String time                   = jObject.getString("time");
 
505
                                                SharedPreferences.Editor edit = prefs.edit();
 
506
                                                edit.putString("vplan_teacher_date", date);
 
507
                                                edit.putString("vplan_teacher_time", time);
 
508
                                                edit.commit();
 
509
                                                } catch(JSONException e) {
 
510
                                                        Log.d("jsonerror", e.getMessage());
 
511
                                                        return new String[] {"json", context.getString(R.string.jsonerror)};
 
512
                                                }
 
513
                                        }
 
514
                                else if(get.equals("noact"))
 
515
                                        return new String[] {"noact", context.getString(R.string.noact)};
 
516
                                else if(get.equals("loginerror"))
 
517
                                        return new String[] {"loginerror", context.getString(R.string.loginerror)};
 
518
                                else if(get.equals("networkerror"))
 
519
                                        return new String[] {"networkerror", context.getString(R.string.networkerror)};
 
520
                                else
 
521
                                        return new String[] {"unknownerror", context.getString(R.string.unknownerror)};
 
522
                                return new String[] {"success", " "};
 
523
                                }
442
524
                }
443
525
        public class VPlanUpdateTask extends AsyncTask<Void, Void, String[]> {
444
526
                protected void onPreExecute() {
448
530
                @Override
449
531
                protected String[] doInBackground(Void... params) {
450
532
                        VPlanUpdater vpup = new VPlanUpdater(VPlan.this);
 
533
                        vpup.updateTeachers();
451
534
                        return vpup.update();
452
535
                }
453
536
                protected void onPostExecute(String[] res) {
472
555
        private ViewPager pager;
473
556
        private SharedPreferences prefs;
474
557
        private ProgressDialog loading;
 
558
        private LinearLayout[] lins = new LinearLayout[3];
475
559
        public void onCreate(Bundle savedInstanceState) {
476
560
                super.onCreate(savedInstanceState);
477
561
                Functions.setTheme(false, true, this);
478
 
        getWindow().setBackgroundDrawableResource(R.layout.background);
 
562
        //getWindow().setBackgroundDrawableResource(R.layout.background);
479
563
                setContentView(R.layout.viewpager);
480
564
            adapter = new VPlanPagerAdapter(this);
481
 
            pager = (ViewPager)findViewById( R.id.viewpager );
 
565
            pager = (ViewPager)findViewById(R.id.viewpager);
482
566
            pager.setOnPageChangeListener(this);
483
567
            pager.setAdapter(adapter);
484
568
            prefs = PreferenceManager.getDefaultSharedPreferences(this);
485
 
        }
 
569
            lins[0] = (LinearLayout) findViewById(R.id.first);
 
570
            lins[1] = (LinearLayout) findViewById(R.id.second);
 
571
            lins[2] = (LinearLayout) findViewById(R.id.third);
 
572
            lins[0].setVisibility(View.VISIBLE);
 
573
            lins[1].setVisibility(View.VISIBLE);
 
574
            lins[2].setVisibility(View.VISIBLE);
 
575
            ((TextView) findViewById(R.id.first_textv)).setText(getString(R.string.mine_short));
 
576
            ((TextView) findViewById(R.id.second_textv)).setText(getString(R.string.all_short));
 
577
            ((TextView) findViewById(R.id.third_textv)).setText(getString(R.string.teachers_short));
 
578
            ((TextView) findViewById(R.id.first_textv)).setOnClickListener(new OnClickListener() {
 
579
                public void onClick(View v) {
 
580
                        pager.setCurrentItem(0);
 
581
                }
 
582
            });
 
583
            ((TextView) findViewById(R.id.second_textv)).setOnClickListener(new OnClickListener() {
 
584
                public void onClick(View v) {
 
585
                        pager.setCurrentItem(1);
 
586
                }
 
587
            });
 
588
            ((TextView) findViewById(R.id.third_textv)).setOnClickListener(new OnClickListener() {
 
589
                public void onClick(View v) {
 
590
                        pager.setCurrentItem(2);
 
591
                }
 
592
            });
 
593
            //pager.setCurrentItem(1, true);
 
594
            //pager.setCurrentItem(0, true);
 
595
            Display display = getWindowManager().getDefaultDisplay();
 
596
            Point size = new Point();
 
597
            display.getSize(size);
 
598
            //int flipper_width = (findViewById(R.id.viewflipper)).getWidth();
 
599
                //Functions.moveViewPagerTitles(lins, 320, 0, 0);
 
600
            }
486
601
        public boolean onCreateOptionsMenu(Menu menu) {
487
602
                MenuInflater inflater = getMenuInflater();
488
603
            inflater.inflate(R.menu.vplan, menu);
552
667
            vpup.execute();
553
668
        }
554
669
        @Override
555
 
        public void onPageScrollStateChanged(int arg0) {
 
670
        public void onPageScrollStateChanged(int state) {
556
671
                //not interesting
557
672
        }
558
673
        @Override
559
 
        public void onPageScrolled(int arg0, float arg1, int arg2) {
560
 
                //not interesting
 
674
        public void onPageScrolled(int position, float offset, int offsetPixels) {
 
675
            lins[0].setVisibility(View.VISIBLE);
 
676
            lins[1].setVisibility(View.VISIBLE);
 
677
            lins[2].setVisibility(View.VISIBLE);
 
678
                int flipper_width = (findViewById(R.id.viewflipper)).getWidth();
 
679
                Functions.moveViewPagerTitles(lins, flipper_width, position, offsetPixels, this);
561
680
        }
562
681
        @Override
563
682
        public void onPageSelected(int position) {
564
 
                if(position == 0)
565
 
                        setTitle(R.string.mine);
566
 
                if(position == 1)
567
 
                        setTitle(R.string.all);
 
683
                //not interesting
568
684
        }
569
685
        @Override
570
686
        public void onDestroy() {