Skip to content

Commit 8fa62d3

Browse files
committed
Merge pull request Catrobat#136 from Catrobat/SDCardFix#40
Fix crash on App startup when no external storage is available
2 parents 53721e0 + 3fd1f1a commit 8fa62d3

File tree

14 files changed

+42
-40
lines changed

14 files changed

+42
-40
lines changed

catroid/res/values-de/strings.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@
273273
<string name="notification_screenshot_ok">Screenshot erfolgreich gespeichert</string>
274274
<string name="notification_load_project_cancel">Projekt laden abgebrochen</string>
275275
<!-- Error messages -->
276-
<string name="error_no_sd_card">Um Catroid zu starten wird eine SD Karte benötigt.\nLegen Sie bitte eine SD Karte ein und versuchen Sie es erneut.</string>
276+
<string name="error_no_writiable_external_storage_available">Um Catroid zu starten wird beschreibbarer externer Speicher wie z.B. eine SD Karte benötigt.\nSie können diesen Fehler beheben indem sie den externen Speicher beschreibbar machen oder eine SD Karte einlegen und es erneut versuchen.</string>
277277
<string name="error_no_name_entered">Bitte einen Namen eingeben.</string>
278278
<string name="error_project_exists">Es gibt bereits ein Projekt mit dem eingegebenen Namen.\nBitte einen anderen Namen eingeben.</string>
279279
<string name="error_project_upload">Beim Upload des Projektes ist ein Fehler aufgetreten.</string>

catroid/res/values-ja/strings.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* GNU Affero General Public License for more details.
2020
*
2121
* You should have received a copy of the GNU Affero General Public License
22-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
-->
2424
<resources>
2525

@@ -57,7 +57,7 @@
5757
<string name="new_project">"新規プロジェクト"</string>
5858
<string name="load">"読み込む"</string>
5959
<string name="about">"キャットロイドについて"</string>
60-
<string name="upload_project">"アップロード"</string>
60+
<string name="upload_project">"アップロード"</string>
6161
-->
6262

6363

@@ -148,7 +148,7 @@
148148
<string name="stagemenu_screen_size">"最大/\nストレッチ"</string>
149149
<string name="stagemenu_axes_on">"軸 オン"</string>
150150
<string name="stagemenu_axes_off">"軸 オフ"</string>
151-
<string name="stagemenu_screenshot">"スクリーン\nショット"</string>
151+
<string name="stagemenu_screenshot">"スクリーン\nショット"</string>
152152
-->
153153

154154

@@ -237,7 +237,7 @@
237237
<string name="notification_invalid_text_entered">"値を入力してください"</string>
238238
<string name="notification_screenshot_ok">"スクリーン ショットが保存した"</string>
239239
<!-- Error messages -->
240-
<string name="error_no_sd_card">"キャットロイドを実行するためにSDカードが必要ですが、どれも見つかりませんでした\nSDカードを挿入して再試行してください"</string>
240+
<string name="error_no_writiable_external_storage_available">"キャットロイドを実行するためにSDカードが必要ですが、どれも見つかりませんでした\nSDカードを挿入して再試行してください"</string>
241241
<string name="error_no_name_entered">"名前を入力してください"</string>
242242
<string name="error_project_exists">"同じ名前のプロジェクトが既に存在しています\n別の名前を選択してください"</string>
243243
<string name="error_project_upload">"アップロード中にエラーが発生しました"</string>

catroid/res/values-ro/strings.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* GNU Affero General Public License for more details.
2020
*
2121
* You should have received a copy of the GNU Affero General Public License
22-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
-->
2424
<resources>
2525

@@ -67,7 +67,7 @@
6767
<string name="load">Incărcare</string>
6868
<string name="change_project_name_main">Schimbarea numelui proiectului</string>
6969
<string name="about">Despre Catroid</string>
70-
<string name="upload_project">Încărcarea proiectului</string>
70+
<string name="upload_project">Încărcarea proiectului</string>
7171
-->
7272

7373

@@ -165,7 +165,7 @@
165165
<!--
166166
<string name="resume_current_project">Reluare</string>
167167
<string name="restart_current_project">Restartare</string>
168-
<string name="snapshot">Screenshot</string>
168+
<string name="snapshot">Screenshot</string>
169169
-->
170170

171171

@@ -251,7 +251,7 @@
251251
<!-- Vă rugăm să introduceți numele -->
252252
<!-- </string> -->
253253
<!-- Error messages -->
254-
<string name="error_no_sd_card">Catroid necesită card SD, dar aceasta nu a fost găsită.\nInserați vă rog cardul SD și încercați din nou.</string>
254+
<string name="error_no_writiable_external_storage_available">Catroid necesită card SD, dar aceasta nu a fost găsită.\nInserați vă rog cardul SD și încercați din nou.</string>
255255
<string name="error_no_name_entered">Introduceți denumirea, vă rog</string>
256256
<string name="error_project_exists">Un proiect cu această denumire deja există.\nIntroduceți altă denumire, vă rog</string>
257257
<!-- <string name="error_create_project"> -->

catroid/res/values-ru/strings.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* GNU Affero General Public License for more details.
2020
*
2121
* You should have received a copy of the GNU Affero General Public License
22-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
-->
2424
<resources>
2525

@@ -67,7 +67,7 @@
6767
<string name="load">Загрузить</string>
6868
<string name="change_project_name_main">Изменить имя проекта</string>
6969
<string name="about">О Catroid</string>
70-
<string name="upload_project">Загрузить проект</string>
70+
<string name="upload_project">Загрузить проект</string>
7171
-->
7272

7373

@@ -175,7 +175,7 @@
175175
Stage menu dialog
176176
<string name="resume_current_project">Возобновить</string>
177177
<string name="restart_current_project">Перезагрузить</string>
178-
<string name="snapshot">Скриншот</string>
178+
<string name="snapshot">Скриншот</string>
179179
-->
180180

181181

@@ -261,7 +261,7 @@
261261
<!-- Пожалуйста, введите имя -->
262262
<!-- </string> -->
263263
<!-- Error messages -->
264-
<string name="error_no_sd_card">Catroid требует наличие SD карты.
264+
<string name="error_no_writiable_external_storage_available">Catroid требует наличие SD карты.
265265
\nПожалуйста введите SD карту и попробуйте еще раз.</string>
266266
<string name="error_no_name_entered">Пожалуйста, введите название.</string>
267267
<string name="error_project_exists">Проект с таким названием уже существуетю\nПожалуйста, выберите другое название.</string>

catroid/res/values/strings.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@
277277
<string name="notification_screenshot_ok">Screenshot saved</string>
278278
<string name="notification_load_project_cancel">Canceled loading project</string>
279279
<!-- Error messages -->
280-
<string name="error_no_sd_card">Catroid requires an SD card in order to run, but none was found.\nPlease insert an SD card and try again.</string>
280+
<string name="error_no_writiable_external_storage_available">Catroid requires writable external storage like an SD card in order to run, but none was found.\nYou can fix this by making the external storage writable or inserting an SD card, then try again.</string>
281281
<string name="error_no_name_entered">Please enter a name.</string>
282282
<string name="error_project_exists">A project with the same name already exists.\nPlease choose a different name.</string>
283283
<string name="error_project_upload">An error occurred while uploading the project.</string>

catroid/src/org/catrobat/catroid/io/StorageHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private StorageHandler() throws IOException {
6161

6262
fullParser = new FullParser();
6363
serializer = new XmlSerializer();
64-
if (!Utils.hasSdCard()) {
64+
if (!Utils.externalStorageAvailable()) {
6565
throw new IOException("Could not read external storage");
6666
}
6767
createCatroidRoot();

catroid/src/org/catrobat/catroid/soundrecorder/SoundRecorderActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void onCreate(Bundle savedInstanceState) {
7171
setViewsToRecordingState();
7272
}
7373

74-
Utils.checkForSdCard(this);
74+
Utils.checkForExternalStorageAvailableAndDisplayErrorIfNot(this);
7575
}
7676

7777
// Code from Stackoverflow to reduce memory problems

catroid/src/org/catrobat/catroid/ui/MainMenuActivity.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ protected void onReceiveResult(int resultCode, Bundle resultData) {
100100
@Override
101101
protected void onCreate(Bundle savedInstanceState) {
102102
super.onCreate(savedInstanceState);
103+
if (!Utils.checkForExternalStorageAvailableAndDisplayErrorIfNot(this)) {
104+
return;
105+
}
103106
Utils.updateScreenWidthAndHeight(this);
104107

105108
setContentView(R.layout.activity_main_menu);
@@ -201,7 +204,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
201204
@Override
202205
protected void onResume() {
203206
super.onResume();
204-
if (!Utils.checkForSdCard(this)) {
207+
if (!Utils.checkForExternalStorageAvailableAndDisplayErrorIfNot(this)) {
205208
return;
206209
}
207210
if (ProjectManager.INSTANCE.getCurrentProject() == null) {
@@ -221,14 +224,18 @@ protected void onResume() {
221224
public void onWindowFocusChanged(boolean hasFocus) {
222225
super.onWindowFocusChanged(hasFocus);
223226

224-
if (projectManager.getCurrentProject() == null) {
227+
if (ProjectManager.getInstance().getCurrentProject() == null) {
225228
return;
226229
}
227230
}
228231

229232
@Override
230233
public void onPause() {
231234
super.onPause();
235+
if (!Utils.externalStorageAvailable()) {
236+
return;
237+
}
238+
232239
// onPause is sufficient --> gets called before "process_killed",
233240
// onStop(), onDestroy(), onRestart()
234241
// also when you switch activities
@@ -245,7 +252,9 @@ public void onPause() {
245252
@Override
246253
protected void onDestroy() {
247254
super.onDestroy();
248-
255+
if (!Utils.externalStorageAvailable()) {
256+
return;
257+
}
249258
unbindDrawables(findViewById(R.id.main_menu));
250259
System.gc();
251260
}

catroid/src/org/catrobat/catroid/ui/fragment/CostumeFragment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public void onSaveInstanceState(Bundle outState) {
163163
@Override
164164
public void onResume() {
165165
super.onResume();
166-
if (!Utils.checkForSdCard(getActivity())) {
166+
if (!Utils.checkForExternalStorageAvailableAndDisplayErrorIfNot(getActivity())) {
167167
return;
168168
}
169169

catroid/src/org/catrobat/catroid/ui/fragment/ScriptFragment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void onStart() {
147147
public void onResume() {
148148
super.onResume();
149149

150-
if (!Utils.checkForSdCard(getActivity())) {
150+
if (!Utils.checkForExternalStorageAvailableAndDisplayErrorIfNot(getActivity())) {
151151
return;
152152
}
153153

catroid/src/org/catrobat/catroid/ui/fragment/SoundFragment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private void startSelectSoundIntent() {
192192
public void onResume() {
193193
super.onResume();
194194

195-
if (!Utils.checkForSdCard(getActivity())) {
195+
if (!Utils.checkForExternalStorageAvailableAndDisplayErrorIfNot(getActivity())) {
196196
return;
197197
}
198198

catroid/src/org/catrobat/catroid/ui/fragment/SpritesListFragment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public void onResume() {
193193
actionMode.finish();
194194
actionMode = null;
195195
}
196-
if (!Utils.checkForSdCard(getActivity())) {
196+
if (!Utils.checkForExternalStorageAvailableAndDisplayErrorIfNot(getActivity())) {
197197
return;
198198
}
199199

catroid/src/org/catrobat/catroid/utils/Utils.java

+9-16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.catrobat.catroid.io.StorageHandler;
4848
import org.catrobat.catroid.ui.dialogs.ErrorDialogFragment;
4949

50+
import android.app.Activity;
5051
import android.app.AlertDialog;
5152
import android.app.AlertDialog.Builder;
5253
import android.content.Context;
@@ -81,30 +82,22 @@ public class Utils {
8182
public static final int FILE_INTENT = 2;
8283
private static boolean isUnderTest;
8384

84-
public static boolean hasSdCard() {
85-
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
85+
public static boolean externalStorageAvailable() {
86+
String externalStorageState = Environment.getExternalStorageState();
87+
return externalStorageState.equals(Environment.MEDIA_MOUNTED)
88+
&& !externalStorageState.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
8689
}
8790

88-
/**
89-
* Checks whether the current device has an SD card. If it has none an error
90-
* message is displayed and the calling activity is finished. A
91-
* RuntimeException is thrown after the call to Activity.finish; find out
92-
* why!
93-
*
94-
* @param context
95-
*/
96-
public static boolean checkForSdCard(final Context context) {
97-
if (!hasSdCard()) {
91+
public static boolean checkForExternalStorageAvailableAndDisplayErrorIfNot(final Context context) {
92+
if (!externalStorageAvailable()) {
9893
Builder builder = new AlertDialog.Builder(context);
9994

10095
builder.setTitle(context.getString(R.string.error));
101-
builder.setMessage(context.getString(R.string.error_no_sd_card));
96+
builder.setMessage(context.getString(R.string.error_no_writiable_external_storage_available));
10297
builder.setNeutralButton(context.getString(R.string.close), new OnClickListener() {
10398
@Override
10499
public void onClick(DialogInterface dialog, int which) {
105-
// finish parent activity
106-
// parentActivity.finish();
107-
System.exit(0);
100+
((Activity) context).moveTaskToBack(true);
108101
}
109102
});
110103
builder.show();

catroidTest/src/org/catrobat/catroid/test/utiltests/UtilsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public void testDeleteSpecialCharactersFromString() {
209209
}
210210

211211
public void testBuildProjectPath() {
212-
if (!Utils.hasSdCard()) {
212+
if (!Utils.externalStorageAvailable()) {
213213
fail("No SD card present");
214214
}
215215
String projectName = "test?Projekt\"1";

0 commit comments

Comments
 (0)