27a4fda1 |
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 | \r |
20 | import java.io.BufferedReader;\r |
21 | import java.io.IOException;\r |
22 | import java.io.InputStreamReader;\r |
23 | \r |
24 | import org.openintents.util.VersionUtils;\r |
25 | \r |
26 | import android.app.Activity;\r |
27 | import android.content.Intent;\r |
28 | import android.content.SharedPreferences;\r |
29 | import android.content.res.Resources;\r |
30 | import android.os.Bundle;\r |
31 | import android.preference.PreferenceManager;\r |
32 | import android.text.TextUtils;\r |
33 | import android.view.View;\r |
34 | import android.widget.Button;\r |
35 | import android.widget.ImageView;\r |
36 | import android.widget.TextView;\r |
37 | \r |
38 | /**\r |
39 | * Displays the Eula for the first time, reading it from a raw resource.\r |
40 | * \r |
41 | * @author Peli\r |
42 | *\r |
43 | */\r |
44 | public class EulaActivity extends Activity {\r |
45 | \r |
46 | Button mAgree;\r |
47 | Button mDisagree;\r |
48 | \r |
49 | String mLaunchPackage;\r |
50 | String mLaunchClass;\r |
51 | Intent mLaunchIntent;\r |
52 | \r |
53 | String mAppName;\r |
54 | \r |
55 | TextView mText1;\r |
56 | TextView mText2;\r |
57 | TextView mText;\r |
58 | ImageView mImage;\r |
59 | \r |
60 | /** Called when the activity is first created. */\r |
61 | @Override\r |
62 | public void onCreate(Bundle icicle) {\r |
63 | super.onCreate(icicle);\r |
64 | \r |
65 | setContentView(R.layout.oi_distribution_eula);\r |
66 | \r |
67 | // Extras are provided by checkEula() below.\r |
68 | Intent i = getIntent();\r |
69 | Bundle b = i.getExtras();\r |
70 | mLaunchPackage = b.getString(EulaOrNewVersion.EXTRA_LAUNCH_ACTIVITY_PACKAGE);\r |
71 | mLaunchClass = b.getString(EulaOrNewVersion.EXTRA_LAUNCH_ACTIVITY_CLASS);\r |
72 | //mLaunchIntent \r |
73 | mLaunchIntent = b.getParcelable(EulaOrNewVersion.EXTRA_LAUNCH_ACTIVITY_INTENT);\r |
74 | \r |
75 | //mIntroContinue = (Button) findViewById(R.id.intro_continue);\r |
76 | mAgree = (Button) findViewById(R.id.button1);\r |
77 | mAgree.setOnClickListener(new View.OnClickListener() {\r |
78 | public void onClick(View view) {\r |
79 | accept();\r |
80 | }\r |
81 | });\r |
82 | \r |
83 | mDisagree = (Button) findViewById(R.id.button2);\r |
84 | mDisagree.setOnClickListener(new View.OnClickListener() {\r |
85 | public void onClick(View view) {\r |
86 | refuse();\r |
87 | }\r |
88 | });\r |
89 | \r |
90 | mText1 = (TextView) findViewById(R.id.text1);\r |
91 | mText2 = (TextView) findViewById(R.id.text2);\r |
92 | mText = (TextView) findViewById(R.id.text);\r |
93 | mImage = (ImageView) findViewById(R.id.imageview);\r |
94 | \r |
95 | mAppName = VersionUtils.getApplicationName(this);\r |
96 | int iconRes = VersionUtils.getApplicationIcon(this);\r |
97 | \r |
98 | setTitle(mAppName);\r |
99 | mImage.setImageResource(iconRes);\r |
100 | \r |
101 | String title = getString(R.string.oi_distribution_eula_title, \r |
102 | mAppName);\r |
103 | String message = getString(R.string.oi_distribution_eula_message, \r |
104 | mAppName);\r |
105 | \r |
106 | mText1.setText(title);\r |
107 | mText2.setText(message);\r |
108 | mText.setText(readTextFromRawResource(R.raw.license_short, false));\r |
109 | }\r |
110 | \r |
111 | \r |
112 | /**\r |
113 | * Accept EULA and proceed with main application.\r |
114 | */\r |
115 | void accept() {\r |
116 | EulaOrNewVersion.storeEulaAccepted(this);\r |
117 | \r |
118 | startOriginalActivity();\r |
119 | }\r |
120 | \r |
121 | \r |
122 | \r |
123 | \r |
124 | void startOriginalActivity() {\r |
125 | // Call the activity that originally called checkEula()\r |
126 | // or checkNewVersion()\r |
127 | Intent i;\r |
128 | if (mLaunchIntent != null) {\r |
129 | i = mLaunchIntent;\r |
130 | \r |
131 | // Android 2.3: category LAUNCHER needs to be removed,\r |
132 | // otherwise main activity is not called.\r |
133 | i.removeCategory(Intent.CATEGORY_LAUNCHER);\r |
134 | } else {\r |
135 | i = new Intent();\r |
136 | i.setClassName(mLaunchPackage, mLaunchClass);\r |
137 | }\r |
138 | i.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);\r |
139 | startActivity(i);\r |
140 | finish();\r |
141 | }\r |
142 | \r |
143 | /**\r |
144 | * Refuse EULA.\r |
145 | */\r |
146 | void refuse() {\r |
147 | finish();\r |
148 | }\r |
149 | \r |
150 | \r |
151 | /**\r |
152 | * Read license from raw resource.\r |
153 | * @param resourceid ID of the raw resource.\r |
154 | * @return\r |
155 | */\r |
156 | String readTextFromRawResource(int resourceid, boolean preserveLineBreaks) {\r |
157 | \r |
158 | // Retrieve license from resource:\r |
159 | String license = "";\r |
160 | Resources resources = getResources();\r |
161 | \r |
162 | //Read in the license file as a big String\r |
163 | BufferedReader in\r |
164 | = new BufferedReader(new InputStreamReader(\r |
165 | resources.openRawResource(resourceid)));\r |
166 | String line;\r |
167 | StringBuilder sb = new StringBuilder();\r |
168 | try {\r |
169 | while ((line = in.readLine()) != null) { // Read line per line.\r |
170 | if (TextUtils.isEmpty(line)) {\r |
171 | // Empty line: Leave line break\r |
172 | sb.append("\n\n");\r |
173 | } else {\r |
174 | sb.append(line);\r |
175 | if (preserveLineBreaks) {\r |
176 | sb.append("\n");\r |
177 | } else {\r |
178 | sb.append(" ");\r |
179 | }\r |
180 | }\r |
181 | }\r |
182 | license = sb.toString();\r |
183 | } catch (IOException e) {\r |
184 | //Should not happen.\r |
185 | e.printStackTrace();\r |
186 | }\r |
187 | \r |
188 | \r |
189 | return license;\r |
190 | }\r |
191 | }\r |