| 1 | /* \r |
| 2 | * Copyright (C) 2007-2011 OpenIntents.org\r |
| 3 | *\r |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");\r |
| 5 | * you may not use this file except in compliance with the License.\r |
| 6 | * You may obtain a copy of the License at\r |
| 7 | *\r |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0\r |
| 9 | *\r |
| 10 | * Unless required by applicable law or agreed to in writing, software\r |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,\r |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r |
| 13 | * See the License for the specific language governing permissions and\r |
| 14 | * limitations under the License.\r |
| 15 | */\r |
| 16 | \r |
| 17 | package org.openintents.filemanager;\r |
| 18 | \r |
| 19 | import android.app.AlertDialog;\r |
| 20 | import android.app.Dialog;\r |
| 21 | import android.content.ActivityNotFoundException;\r |
| 22 | import android.content.Context;\r |
| 23 | import android.content.DialogInterface;\r |
| 24 | import android.content.DialogInterface.OnClickListener;\r |
| 25 | import android.content.Intent;\r |
| 26 | import android.net.Uri;\r |
| 27 | import android.util.Log;\r |
| 28 | import android.view.View;\r |
| 29 | import android.widget.Toast;\r |
| 30 | \r |
| 31 | /**\r |
| 32 | * @version 2009-02-04\r |
| 33 | * @version 2011-02-07: Allow for string arguments.\r |
| 34 | * @author Peli\r |
| 35 | */\r |
| 36 | public class DownloadAppDialog extends AlertDialog implements OnClickListener {\r |
| 37 | private static final String TAG = "StartSaveActivity";\r |
| 38 | \r |
| 39 | Context mContext;\r |
| 40 | String mDownloadAppName;\r |
| 41 | String mDownloadPackageName;\r |
| 42 | String mDownloadWebsite;\r |
| 43 | String mMessageText;\r |
| 44 | \r |
| 45 | boolean mMarketAvailable;\r |
| 46 | \r |
| 47 | public DownloadAppDialog(Context context) {\r |
| 48 | super(context);\r |
| 49 | mContext = context;\r |
| 50 | }\r |
| 51 | \r |
| 52 | public DownloadAppDialog(Context context, int message_id, int download_name_id, int download_package_id, int download_website_id) {\r |
| 53 | super(context);\r |
| 54 | mContext = context;\r |
| 55 | set(message_id, download_name_id, download_package_id, download_website_id);\r |
| 56 | }\r |
| 57 | \r |
| 58 | public DownloadAppDialog(Context context, String message, String download_name, String download_package, String download_website) {\r |
| 59 | super(context);\r |
| 60 | mContext = context;\r |
| 61 | set(message, download_name, download_package, download_website);\r |
| 62 | }\r |
| 63 | \r |
| 64 | protected void set(int message_id, int download_name_id,\r |
| 65 | int download_package_id, int download_website_id) {\r |
| 66 | String message = mContext.getString(message_id);\r |
| 67 | String download_name = mContext.getString(download_name_id);\r |
| 68 | String download_package = mContext.getString(download_package_id);\r |
| 69 | String download_website = mContext.getString(download_website_id);\r |
| 70 | set(message, download_name, download_package, download_website);\r |
| 71 | }\r |
| 72 | \r |
| 73 | protected void set(String message, String download_name,\r |
| 74 | String download_package, String download_website) {\r |
| 75 | mDownloadAppName = download_name;\r |
| 76 | mDownloadPackageName = download_package;\r |
| 77 | mDownloadWebsite = download_website;\r |
| 78 | \r |
| 79 | mMarketAvailable = MarketUtils.isMarketAvailable(mContext, mDownloadPackageName);\r |
| 80 | \r |
| 81 | StringBuilder sb = new StringBuilder();\r |
| 82 | sb.append(message);\r |
| 83 | sb.append(" ");\r |
| 84 | if (mMarketAvailable) {\r |
| 85 | sb.append(mContext.getString(R.string.oi_distribution_download_market_message, \r |
| 86 | mDownloadAppName));\r |
| 87 | } else {\r |
| 88 | sb.append(mContext.getString(R.string.oi_distribution_download_message, \r |
| 89 | mDownloadAppName));\r |
| 90 | }\r |
| 91 | mMessageText = sb.toString();\r |
| 92 | setMessage(mMessageText);\r |
| 93 | \r |
| 94 | setTitle(mContext.getString(R.string.oi_distribution_download_title,\r |
| 95 | mDownloadAppName));\r |
| 96 | \r |
| 97 | setButton(mContext.getText(R.string.oi_distribution_download_market), this);\r |
| 98 | setButton2(mContext.getText(R.string.oi_distribution_download_web), this);\r |
| 99 | setButton3(mContext.getText(android.R.string.cancel), this);\r |
| 100 | }\r |
| 101 | \r |
| 102 | public void onClick(DialogInterface dialog, int which) {\r |
| 103 | Intent intent;\r |
| 104 | \r |
| 105 | if (which == BUTTON1) {\r |
| 106 | intent = MarketUtils.getMarketDownloadIntent(mDownloadPackageName);\r |
| 107 | startSaveActivity(intent);\r |
| 108 | } else if (which == BUTTON2) {\r |
| 109 | intent = new Intent(Intent.ACTION_VIEW);\r |
| 110 | Uri uri= Uri.parse(mDownloadWebsite);\r |
| 111 | intent.setData(uri);\r |
| 112 | startSaveActivity(intent);\r |
| 113 | }\r |
| 114 | }\r |
| 115 | \r |
| 116 | public static void onPrepareDialog(Context context, Dialog dialog) {\r |
| 117 | DownloadAppDialog d = (DownloadAppDialog) dialog;\r |
| 118 | \r |
| 119 | boolean has_android_market = MarketUtils.isMarketAvailable(context, d.mDownloadPackageName);\r |
| 120 | \r |
| 121 | dialog.findViewById(android.R.id.button1).setVisibility(\r |
| 122 | has_android_market ? View.VISIBLE : View.GONE);\r |
| 123 | }\r |
| 124 | \r |
| 125 | /**\r |
| 126 | * Start an activity but prompt a toast if activity is not found\r |
| 127 | * (instead of crashing).\r |
| 128 | * \r |
| 129 | * @param context\r |
| 130 | * @param intent\r |
| 131 | */\r |
| 132 | public void startSaveActivity(Intent intent) {\r |
| 133 | try {\r |
| 134 | mContext.startActivity(intent);\r |
| 135 | } catch (ActivityNotFoundException e) {\r |
| 136 | Toast.makeText(mContext,\r |
| 137 | R.string.oi_distribution_update_error,\r |
| 138 | Toast.LENGTH_SHORT).show();\r |
| 139 | Log.e(TAG, "Error starting second activity.", e);\r |
| 140 | }\r |
| 141 | }\r |
| 142 | }\r |