summaryrefslogtreecommitdiff
path: root/java/demo/android/library/src/com/zeroc/library/SessionActivity.java
blob: 4c5c0f3cf0a89ea749b22d0536cfe53f82657748 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// **********************************************************************
//
// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package com.zeroc.library;

import com.zeroc.library.controller.QueryController;
import com.zeroc.library.controller.SessionController;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;

// The base class of any activity created after a session has been established.
public class SessionActivity extends Activity
{
    protected static final int DIALOG_ERROR = 0;
    protected static final int DIALOG_FATAL = 1;
    protected static final int DIALOG_NEXT = 2;

    protected SessionController _sessionController;
    protected QueryController _queryController;

    @Override
    public void onResume()
    {
        super.onResume();

        LibraryApp app = (LibraryApp)getApplication();
        _sessionController = app.getSessionController();
        if(_sessionController == null)
        {
            finish();
            return;
        }

        _sessionController.setSessionListener(new SessionController.Listener()
        {
            public void onDestroy()
            {
                showDialog(DIALOG_FATAL);
            }
        });

        _queryController = _sessionController.getCurrentQuery();
    }

    @Override
    protected Dialog onCreateDialog(final int id)
    {
        switch (id)
        {
        case DIALOG_ERROR:
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Error");
            builder.setMessage(""); // Filled in onPrepareDialog.
            builder.setCancelable(false);
            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int whichButton)
                {
                    _queryController.clearLastError();
                }
            });
            return builder.create();
        }

        case DIALOG_FATAL:
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Error");
            builder.setMessage("The session was lost. Please login again.");
            builder.setCancelable(false);
            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int whichButton)
                {
                    LibraryApp app = (LibraryApp)getApplication();
                    app.logout();
                    finish();
                }
            });
            return builder.create();
        }
        }

        return null;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog)
    {
        super.onPrepareDialog(id, dialog);
        if(id == DIALOG_ERROR)
        {
            AlertDialog alert = (AlertDialog)dialog;
            alert.setMessage(_queryController.getLastError());
        }
    }
}