merge in OI distribution
[android_pandora.git] / apps / oi-filemanager / FileManager / src / org / openintents / filemanager / InfoActivity.java
1 package org.openintents.filemanager;
2
3 import org.openintents.util.VersionUtils;
4
5 import android.app.AlertDialog;
6 import android.app.Dialog;
7 import android.content.ActivityNotFoundException;
8 import android.content.Context;
9 import android.content.DialogInterface;
10 import android.content.Intent;
11 import android.net.Uri;
12 import android.os.Bundle;
13 import android.view.Menu;
14 import android.view.View;
15 import android.view.ViewGroup;
16 import android.widget.AdapterView;
17 import android.widget.AdapterView.OnItemClickListener;
18 import android.widget.ArrayAdapter;
19 import android.widget.ListView;
20 import android.widget.TextView;
21 import android.widget.Toast;
22
23 public class InfoActivity extends DistributionLibraryListActivity implements OnItemClickListener {
24
25         // Override the following variables in init()
26     protected int[] mApplications = {};
27     protected String[] mPackageNames = {};
28     protected int[] mMinVersionCodes = {};
29     protected String[] mMinVersionName = {};
30     protected int[] mInfoText = {};
31     protected String[] mDeveloperUris = {};
32     protected String[] mIntentAction = {};
33     protected String[] mIntentData = {};
34     
35     //
36         
37         private static final int MENU_DISTRIBUTION_START = Menu.FIRST + 100; // MUST BE LAST
38
39         public static final int DIALOG_INFO = 0;
40         public static final int DIALOG_GET_FROM_MARKET = 100;
41         private static final int DIALOG_DISTRIBUTION_START = 200; // MUST BE LAST
42
43         
44     private String[] mApplicationStrings;
45
46     @Override
47     public void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49
50         mDistribution.setFirst(MENU_DISTRIBUTION_START, DIALOG_DISTRIBUTION_START);
51         
52         // Check whether EULA has been accepted
53         // or information about new version can be presented.
54         if (mDistribution.showEulaOrNewVersion()) {
55             return;
56         }
57         
58         setContentView(R.layout.oi_distribution_infoactivity);
59
60         init();
61         
62         mApplicationStrings = new String[mApplications.length];
63         for (int i = 0; i < mApplications.length; i++) {
64                 mApplicationStrings[i] = getString(mApplications[i]);
65         }
66         setListAdapter(new FontArrayAdapter<String>(this,
67                 android.R.layout.simple_list_item_1, mApplicationStrings));
68         ListView listview = getListView();
69         
70         listview.setOnItemClickListener(this);
71         
72         // Set message of activity
73         String appname = VersionUtils.getApplicationName(this);
74                 String message = getString(R.string.oi_distribution_info_activity_text, 
75                                 appname);
76         TextView tv = (TextView) findViewById(R.id.text);
77         tv.setText(message);
78         
79         /*
80         TypedArray a = obtainStyledAttributes(mTheme, R.styleable.ShoppingList);
81                 String typefaceName = a.getString(R.styleable.ShoppingList_textTypeface);
82             mTextSizeMedium = a.getDimensionPixelOffset(R.styleable.ShoppingList_textSizeMedium, 23);
83             mTextSizeLarge = a.getDimensionPixelOffset(R.styleable.ShoppingList_textSizeLarge, 28);
84             mTextColor = a.getColor(R.styleable.ShoppingList_textColor, Color.BLACK);
85             Drawable background = a.getDrawable(R.styleable.ShoppingList_background);
86
87             
88             View v = findViewById(R.id.background);
89             v.setBackgroundDrawable(background);
90             
91                 mTypeface = Typeface.createFromAsset(getResources().getAssets(), typefaceName);
92         
93         TextView tv = (TextView) findViewById(R.id.text);
94         tv.setTypeface(mTypeface);
95         tv.setTextSize(mTextSizeMedium);
96         tv.setTextColor(mTextColor);
97             */
98     }
99
100     public void init() {
101         
102     }
103
104         public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
105                 if (VersionUtils.isPackageAvailable(this, mPackageNames[pos], mMinVersionCodes[pos])) {
106                         showDialog(DIALOG_INFO + pos);
107                 } else {
108                         showDialog(DIALOG_GET_FROM_MARKET + pos);
109                 }
110         }
111         
112         private class FontArrayAdapter<T> extends ArrayAdapter<T> {
113
114                 public FontArrayAdapter(Context context, int textViewResourceId,
115                                 T[] objects) {
116                         super(context, textViewResourceId, objects);
117                 }
118
119                 @Override
120                 public View getView(int position, View convertView, ViewGroup parent) {
121                         // TODO Auto-generated method stub
122                         TextView tv = (TextView) super.getView(position, convertView, parent);
123                         /*
124                         tv.setTypeface(mTypeface);
125                         tv.setTextSize(mTextSizeLarge);
126                 tv.setTextColor(mTextColor);
127                         */
128                         return tv;
129                 }
130         }
131
132
133         @Override
134         protected Dialog onCreateDialog(int id) {
135                 Dialog dialog = super.onCreateDialog(id);
136                 
137                 if (dialog != null) {
138                         return dialog;
139                 }
140                 
141                 if (id >= DIALOG_INFO && id < DIALOG_GET_FROM_MARKET) {
142                         dialog = buildInfoDialog(id - DIALOG_INFO);
143                 } else if (id >= DIALOG_GET_FROM_MARKET && id < DIALOG_DISTRIBUTION_START){
144                         dialog = buildGetFromMarketDialog(id - DIALOG_GET_FROM_MARKET);
145                 }
146                 /*if (dialog == null) {
147                         dialog = super.onCreateDialog(id);
148                 }*/
149                 return dialog;
150         }
151
152         @Override
153         protected void onPrepareDialog(int id, Dialog dialog) {
154                 super.onPrepareDialog(id, dialog);
155                 if (id >= DIALOG_INFO && id < DIALOG_GET_FROM_MARKET) {
156                         dialog.findViewById(android.R.id.button1).setVisibility(View.GONE);
157                 } else if (id >= DIALOG_GET_FROM_MARKET && id < DIALOG_DISTRIBUTION_START){
158                         DownloadAppDialog.onPrepareDialog(this, dialog);
159                 }
160                 
161         }
162
163         private AlertDialog buildInfoDialog(final int pos) {
164                 String infotext = getString(mInfoText[pos], mApplicationStrings[pos]);
165                 String infolaunch = getString(R.string.oi_distribution_info_launch, mApplicationStrings[pos]);
166                 
167                 // Trick for Android 2.3:
168                 // To achieve the visual trick of extending the button over the whole width,
169                 // we first set a positive button, and then set its visibility to GONE in 
170                 // onPrepareDialog().
171                 
172                 return new AlertDialog.Builder(this)
173                         .setIcon(android.R.drawable.ic_dialog_info)
174                         .setTitle(R.string.oi_distribution_info_instructions)
175                         .setMessage(infotext)
176                         .setPositiveButton("", null)
177                         .setNegativeButton(infolaunch,
178                                         new DialogInterface.OnClickListener() {
179                                                 public void onClick(DialogInterface dialog,
180                                                                 int whichButton) {
181                                                         // click Ok
182                                                         launchApplication(pos);
183                                                 }
184                                         })
185                         .create();
186         }
187
188         void launchApplication(int pos) {
189                 Intent intent = new Intent();
190                 intent.setAction(mIntentAction[pos]);
191                 if (mIntentAction[pos].equals(Intent.ACTION_MAIN)) {
192                         // Exception for ACTION_MAIN:
193                         // Use data as class name.
194                         if (mPackageNames[pos] != null && mIntentData[pos] != null) {
195                                 intent.setClassName(mPackageNames[pos], mIntentData[pos]);
196                         }
197                 } else {
198                         if (mIntentData[pos] != null) {
199                                 intent.setData(Uri.parse(mIntentData[pos]));
200                         }
201                 }
202                 
203                 try {
204                         startActivity(intent);
205                 } catch (ActivityNotFoundException e) {
206
207                         Toast.makeText(this,
208                                         R.string.oi_distribution_launch_error,
209                                         Toast.LENGTH_SHORT).show();
210                 }
211         }
212
213         private AlertDialog buildGetFromMarketDialog(int pos) {
214                 String this_appname = VersionUtils.getApplicationName(this);
215                 String info_not_available = getString(R.string.oi_distribution_info_not_available, 
216                                 this_appname, mApplicationStrings[pos], mMinVersionName[pos]);
217                 String download_appname = getString(mApplications[pos]);
218                 
219                 return new DownloadAppDialog(this, 
220                                 info_not_available, 
221                                 download_appname, 
222                                 mPackageNames[pos], 
223                                 mDeveloperUris[pos]);
224         }
225 }