root / src / com / rackspacecloud / android / TabViewActivity.java @ 48601850
History | View | Annotate | Download (3.5 kB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.rackspacecloud.android; |
5 |
|
6 |
import java.util.Calendar; |
7 |
|
8 |
import android.app.Dialog; |
9 |
import android.app.TabActivity; |
10 |
import android.content.Context; |
11 |
import android.content.Intent; |
12 |
import android.os.Bundle; |
13 |
import android.view.View; |
14 |
import android.view.View.OnClickListener; |
15 |
import android.widget.Button; |
16 |
import android.widget.EditText; |
17 |
import android.widget.TabHost; |
18 |
import android.widget.Toast; |
19 |
|
20 |
import com.rackspace.cloud.android.R; |
21 |
|
22 |
/**
|
23 |
* @author Mike Mayo - mike.mayo@rackspace.com - twitter.com/greenisus
|
24 |
*
|
25 |
*/
|
26 |
public class TabViewActivity extends TabActivity { |
27 |
|
28 |
protected AndroidCloudApplication app;
|
29 |
private final int PASSWORD_PROMPT = 123; |
30 |
private boolean loggedIn; |
31 |
|
32 |
@Override
|
33 |
public void onCreate(Bundle savedInstanceState) { |
34 |
super.onCreate(savedInstanceState);
|
35 |
|
36 |
TabHost tabs = getTabHost(); |
37 |
|
38 |
TabHost.TabSpec spec = tabs.newTabSpec("tab1");
|
39 |
spec.setContent(new Intent(this, ListServersActivity.class)); |
40 |
spec.setIndicator("Cloud Servers", getResources().getDrawable(R.drawable.cloudservers_icon));
|
41 |
tabs.addTab(spec); |
42 |
|
43 |
spec = tabs.newTabSpec("tab2");
|
44 |
spec.setContent(new Intent(this, ListContainerActivity.class)); |
45 |
spec.setIndicator("Cloud Files", getResources().getDrawable(R.drawable.cloudfiles));
|
46 |
tabs.addTab(spec); |
47 |
|
48 |
spec = tabs.newTabSpec("tab3");
|
49 |
spec.setContent(new Intent(this, ListLoadBalancersActivity.class)); |
50 |
spec.setIndicator("Load Balancers", getResources().getDrawable(R.drawable.load_balancers_icon));
|
51 |
tabs.addTab(spec); |
52 |
} |
53 |
|
54 |
@Override
|
55 |
protected void onPause(){ |
56 |
super.onPause();
|
57 |
Calendar cal = Calendar.getInstance(); |
58 |
AndroidCloudApplication.lastPause = cal.getTimeInMillis(); |
59 |
} |
60 |
|
61 |
@Override
|
62 |
protected void onResume(){ |
63 |
super.onResume();
|
64 |
Calendar cal = Calendar.getInstance(); |
65 |
long curTime = cal.getTimeInMillis();
|
66 |
if(curTime - AndroidCloudApplication.lastPause > 5000){ |
67 |
verifyPassword(); |
68 |
} |
69 |
} |
70 |
|
71 |
/*
|
72 |
* if the application is password protected,
|
73 |
* the user must provide the password before
|
74 |
* gaining access
|
75 |
*/
|
76 |
private void verifyPassword(){ |
77 |
PasswordManager pwManager = new PasswordManager(getSharedPreferences(
|
78 |
Preferences.SHARED_PREFERENCES_NAME, MODE_PRIVATE));
|
79 |
if(pwManager.hasPassword() && !loggedIn){
|
80 |
createCustomDialog(PASSWORD_PROMPT); |
81 |
} |
82 |
} |
83 |
|
84 |
private boolean rightPassword(String password){ |
85 |
PasswordManager pwManager = new PasswordManager(getSharedPreferences(
|
86 |
Preferences.SHARED_PREFERENCES_NAME, MODE_PRIVATE));
|
87 |
return pwManager.verifyEnteredPassword(password);
|
88 |
} |
89 |
|
90 |
|
91 |
/*
|
92 |
* forces the user to enter a correct password
|
93 |
* before they gain access to application data
|
94 |
*/
|
95 |
private void createCustomDialog(int id) { |
96 |
final Dialog dialog = new Dialog(TabViewActivity.this); |
97 |
switch (id) {
|
98 |
case PASSWORD_PROMPT:
|
99 |
dialog.setContentView(R.layout.passworddialog); |
100 |
dialog.setTitle("Enter your password:");
|
101 |
dialog.setCancelable(false);
|
102 |
Button button = (Button) dialog.findViewById(R.id.submit_password); |
103 |
button.setOnClickListener(new OnClickListener() {
|
104 |
public void onClick(View v){ |
105 |
EditText passwordText = ((EditText)dialog.findViewById(R.id.submit_password_text)); |
106 |
if(!rightPassword(passwordText.getText().toString())){
|
107 |
passwordText.setText("");
|
108 |
showToast("Password was incorrect.");
|
109 |
loggedIn = false;
|
110 |
} |
111 |
else{
|
112 |
dialog.dismiss(); |
113 |
loggedIn = true;
|
114 |
} |
115 |
} |
116 |
|
117 |
}); |
118 |
dialog.show(); |
119 |
} |
120 |
} |
121 |
|
122 |
protected void showToast(String message) { |
123 |
Context context = getApplicationContext();
|
124 |
int duration = Toast.LENGTH_SHORT;
|
125 |
Toast toast = Toast.makeText(context, message, duration); |
126 |
toast.show(); |
127 |
} |
128 |
|
129 |
} |