1 package org.openintents.filemanager;
3 import org.openintents.util.VersionUtils;
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;
23 public class InfoActivity extends DistributionLibraryListActivity implements OnItemClickListener {
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 = {};
37 private static final int MENU_DISTRIBUTION_START = Menu.FIRST + 100; // MUST BE LAST
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
44 private String[] mApplicationStrings;
47 public void onCreate(Bundle savedInstanceState) {
48 super.onCreate(savedInstanceState);
50 mDistribution.setFirst(MENU_DISTRIBUTION_START, DIALOG_DISTRIBUTION_START);
52 // Check whether EULA has been accepted
53 // or information about new version can be presented.
54 if (mDistribution.showEulaOrNewVersion()) {
58 setContentView(R.layout.oi_distribution_infoactivity);
62 mApplicationStrings = new String[mApplications.length];
63 for (int i = 0; i < mApplications.length; i++) {
64 mApplicationStrings[i] = getString(mApplications[i]);
66 setListAdapter(new FontArrayAdapter<String>(this,
67 android.R.layout.simple_list_item_1, mApplicationStrings));
68 ListView listview = getListView();
70 listview.setOnItemClickListener(this);
72 // Set message of activity
73 String appname = VersionUtils.getApplicationName(this);
74 String message = getString(R.string.oi_distribution_info_activity_text,
76 TextView tv = (TextView) findViewById(R.id.text);
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);
88 View v = findViewById(R.id.background);
89 v.setBackgroundDrawable(background);
91 mTypeface = Typeface.createFromAsset(getResources().getAssets(), typefaceName);
93 TextView tv = (TextView) findViewById(R.id.text);
94 tv.setTypeface(mTypeface);
95 tv.setTextSize(mTextSizeMedium);
96 tv.setTextColor(mTextColor);
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);
108 showDialog(DIALOG_GET_FROM_MARKET + pos);
112 private class FontArrayAdapter<T> extends ArrayAdapter<T> {
114 public FontArrayAdapter(Context context, int textViewResourceId,
116 super(context, textViewResourceId, objects);
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);
124 tv.setTypeface(mTypeface);
125 tv.setTextSize(mTextSizeLarge);
126 tv.setTextColor(mTextColor);
134 protected Dialog onCreateDialog(int id) {
135 Dialog dialog = super.onCreateDialog(id);
137 if (dialog != null) {
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);
146 /*if (dialog == null) {
147 dialog = super.onCreateDialog(id);
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);
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]);
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().
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,
182 launchApplication(pos);
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]);
198 if (mIntentData[pos] != null) {
199 intent.setData(Uri.parse(mIntentData[pos]));
204 startActivity(intent);
205 } catch (ActivityNotFoundException e) {
208 R.string.oi_distribution_launch_error,
209 Toast.LENGTH_SHORT).show();
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]);
219 return new DownloadAppDialog(this,
223 mDeveloperUris[pos]);