From 66cdf8e9d9627fd3ca24fbb12a0f652cdfd3b2b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Droz?= Date: Mon, 17 Apr 2017 00:55:33 -0300 Subject: [PATCH] (not compiling) initial attempt to grab an IconSet from a remote location and store it to cache afterward. * Internal Storage => no additionnal permission * Fetch from one URL (a XML file) so anyone can create/host its sets * Expected XML would contains: Title The URL of the PNG file defining the verso The URL of a PNG file for an icon [...] ATM the Tile* class use int from Resources rather than Drawable, that may need to be changed. --- local.properties | 10 -- .../games/memory/kids/IconSet.java | 79 +++++++++ .../games/memory/kids/IconSetList.java | 158 ++++++++++++++++++ .../games/memory/kids/PreferencesService.java | 14 +- .../games/memory/kids/ui/MainActivity.java | 59 +------ .../memory/kids/ui/PreferencesActivity.java | 4 +- 6 files changed, 250 insertions(+), 74 deletions(-) delete mode 100644 local.properties create mode 100644 src/main/java/org/androidsoft/games/memory/kids/IconSet.java create mode 100644 src/main/java/org/androidsoft/games/memory/kids/IconSetList.java diff --git a/local.properties b/local.properties deleted file mode 100644 index 324119d..0000000 --- a/local.properties +++ /dev/null @@ -1,10 +0,0 @@ -# This file is automatically generated by Android Tools. -# Do not modify this file -- YOUR CHANGES WILL BE ERASED! -# -# This file must *NOT* be checked into Version Control Systems, -# as it contains information specific to your local configuration. - -# location of the SDK. This is only used by Ant -# For customization when using a Version Control System, please read the -# header note. -sdk.dir=/home/pierre/java/libs/android-sdk-linux diff --git a/src/main/java/org/androidsoft/games/memory/kids/IconSet.java b/src/main/java/org/androidsoft/games/memory/kids/IconSet.java new file mode 100644 index 0000000..2e2889c --- /dev/null +++ b/src/main/java/org/androidsoft/games/memory/kids/IconSet.java @@ -0,0 +1,79 @@ +/* Copyright (c) 2017 Raphaël Droz + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.androidsoft.games.memory.kids; + +import java.util.List; +import java.util.ArrayList; +import android.R.drawable; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.BitmapDrawable; +import android.content.Context; + + + +public class IconSet +{ + // instance properties + public String title; + + // only when fetched remotely, optionnal if + // image does not have corresponding path + public String verso_path; + public String[] images_path; + + // when actually loaded, optionnal when the IconSet + // is in a transitory state (manifest file loaded, but image are not */ + public BitmapDrawable verso; + public List images = new ArrayList(); + + + // constructor from filepath/URL + // http://stackoverflow.com/a/9490060 + public IconSet(String title, String verso_path, List images_path, Context ctx) { + if (title.equals("") || verso_path == null || images_path.size() == 0) { + return; + } + + this.title = title; + this.verso_path = verso_path; + this.images_path = images_path.toArray(); + this.verso = new BitmapDrawable(ctx.getResources(), verso_path); + this.images = new ArrayList(images_path.size()); + for (String temp : images_path) { + this.images.add(new BitmapDrawable(ctx.getResources(), temp)); + + } + } + + // useful constructor for the default IconSet + public IconSet(String title, int verso, int[] images) { + this.title = title; + this.verso = verso; + this.images = (ArrayList)new ArrayList(images); + } + + public List getImages() { + return images; + } + + public BitmapDrawable getVerso() { + return verso; + } + + public String getTitle() { + return title; + } +} diff --git a/src/main/java/org/androidsoft/games/memory/kids/IconSetList.java b/src/main/java/org/androidsoft/games/memory/kids/IconSetList.java new file mode 100644 index 0000000..8768036 --- /dev/null +++ b/src/main/java/org/androidsoft/games/memory/kids/IconSetList.java @@ -0,0 +1,158 @@ +/* Copyright (c) 2017 Raphaël Droz + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.androidsoft.games.memory.kids; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import java.net.URL; +import java.net.URLConnection; +import java.util.HashMap; +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; +import java.io.File; +import java.io.IOException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.net.ssl.HttpsURLConnection; +import android.R.drawable; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.BitmapDrawable; +import android.widget.ImageView; +import android.content.Context; +import android.net.Uri; + +import org.androidsoft.games.memory.kids.IconSet; + +public class IconSetList +{ + + + // properties of the set of icon sets + public static final String DEFAULT_SET_NAME = "easter"; + + private static HashMap set; + private static HashMap default_set; + + static int[] tiles_default = {R.drawable.default_1, R.drawable.default_2, + R.drawable.default_3, R.drawable.default_4, R.drawable.default_5, R.drawable.default_6, + R.drawable.default_7, R.drawable.default_8, R.drawable.default_9, R.drawable.default_10, + R.drawable.default_11, R.drawable.default_12, R.drawable.default_13, R.drawable.default_14, + R.drawable.default_15, R.drawable.default_16, R.drawable.default_17, R.drawable.default_18, + R.drawable.default_19, R.drawable.default_20, R.drawable.default_21, R.drawable.default_22, + R.drawable.default_23, R.drawable.default_24, R.drawable.default_25, R.drawable.default_26, + R.drawable.default_27, R.drawable.default_28, R.drawable.default_29, R.drawable.default_30, + R.drawable.default_31, R.drawable.default_32, R.drawable.default_33, R.drawable.default_34}; + + static int[] tiles_christmas = {R.drawable.christmas_1, R.drawable.christmas_2, + R.drawable.christmas_3, R.drawable.christmas_4, R.drawable.christmas_5, R.drawable.christmas_6, + R.drawable.christmas_7, R.drawable.christmas_8, R.drawable.christmas_9, R.drawable.christmas_10, + R.drawable.christmas_11, R.drawable.christmas_12, R.drawable.christmas_13, R.drawable.christmas_14, + R.drawable.christmas_15, R.drawable.christmas_16, R.drawable.christmas_17, R.drawable.christmas_18, + R.drawable.christmas_19, R.drawable.christmas_20, R.drawable.christmas_21, R.drawable.christmas_22}; + + static int[] tiles_easter = {R.drawable.easter_1, R.drawable.easter_2, + R.drawable.easter_3, R.drawable.easter_4, R.drawable.easter_5, R.drawable.easter_6, + R.drawable.easter_7, R.drawable.easter_8, R.drawable.easter_9, R.drawable.easter_10, + R.drawable.easter_11, R.drawable.easter_12, R.drawable.easter_13, R.drawable.easter_14}; + + static int[] tiles_tux = {R.drawable.tux_1, R.drawable.tux_2, + R.drawable.tux_3, R.drawable.tux_4, R.drawable.tux_5, R.drawable.tux_6, + R.drawable.tux_7, R.drawable.tux_8, R.drawable.tux_9, R.drawable.tux_10, + R.drawable.tux_11, R.drawable.tux_12, R.drawable.tux_13, R.drawable.tux_14, + R.drawable.tux_15, R.drawable.tux_16, R.drawable.tux_17, R.drawable.tux_18, + R.drawable.tux_19, R.drawable.tux_20, R.drawable.tux_21, R.drawable.tux_22, + R.drawable.tux_23, R.drawable.tux_24, R.drawable.tux_25, R.drawable.tux_26, + R.drawable.tux_27, R.drawable.tux_28, R.drawable.tux_29, R.drawable.tux_30, + R.drawable.tux_31, R.drawable.tux_32, R.drawable.tux_33}; + + static { + HashMap aset = new HashMap(); + aset.put("default", new IconSet("default", R.drawable.not_found_default, tiles_default)); + aset.put("christmas", new IconSet("christmas", R.drawable.not_found_christmas, tiles_christmas)); + aset.put("easter", new IconSet("easter", R.drawable.not_found_easter, tiles_easter)); + aset.put("tux", new IconSet("tux", R.drawable.not_found_tux, tiles_tux)); + default_set.putAll(aset); + set.putAll(aset); + } + + /* apply to the various IconSet */ + public static IconSet get(String title) { + return set.get(title); + } + + public static IconSet getDefault() { + return set.get(DEFAULT_SET_NAME); + } + + public static boolean addFromUrl(URL url, Context ctx) { + IconSet x = loadFromUrl(url, true, ctx); + if (x != null) { + set.put(x.getTitle(), x); + return true; + } + return false; + } + + // http://stackoverflow.com/a/8897653 + public static IconSet loadFromUrl(URL url, boolean store, Context context) { + try { + String t, v; + List img = new ArrayList(); + + URLConnection conn = url.openConnection(); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(conn.getInputStream()); + + Element title_element = doc.getElementById("title"); + t = title_element.getTextContent(); + + Element verso_element = doc.getElementById("verso"); + if (verso_element != null) { + v = verso_element.getTextContent(); + } + + NodeList nodes = doc.getElementsByTagName("images"); + for (int i = 0; i < nodes.getLength(); i++) { + Element element = (Element) nodes.item(i); + NodeList image_element = element.getElementsByTagName("image"); + Element image = (Element) image_element.item(0); + img.add(image.getTextContent()); + if (store == true) { + // https://developer.android.com/training/basics/data-storage/files.html + File file = File.createTempFile(Uri.parse(image.getTextContent()).getLastPathSegment(), null, context.getCacheDir()); + } + } + + IconSet is = new IconSet(t, v, img, context); + // add to the currently usable IconSets + set.put(t, is); + return is; + } + catch (IOException e) { + e.printStackTrace(); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + public static void loadAdditionalIconSets(/*TODO*/) { + } +} diff --git a/src/main/java/org/androidsoft/games/memory/kids/PreferencesService.java b/src/main/java/org/androidsoft/games/memory/kids/PreferencesService.java index c737c82..85b23b0 100644 --- a/src/main/java/org/androidsoft/games/memory/kids/PreferencesService.java +++ b/src/main/java/org/androidsoft/games/memory/kids/PreferencesService.java @@ -16,6 +16,8 @@ import android.content.Context; import android.content.SharedPreferences; +import org.androidsoft.games.memory.kids.IconSet; +import org.androidsoft.games.memory.kids.IconSetList; /** * Preference Service @@ -25,9 +27,8 @@ public class PreferencesService { - public static final int ICONS_SET_NORMAL = 0; - public static final int ICONS_SET_SEASON = 1; public static final int HISCORE_DEFAULT = 200; + private static final String PREFS_NAME = "MemoryPrefsFile"; private static final String PREF_BEST_MOVE_COUNT = "best_move_count"; private static final String PREF_SOUND_ENABLED = "sound_enabled"; @@ -87,18 +88,17 @@ public void saveSoundEnabled(boolean enabled) editor.apply(); } - public void saveIconsSet(int set) + public void saveIconsSet(String set) { SharedPreferences.Editor editor = getPrefs().edit(); - editor.putInt(PREF_ICONS_SET, set); + editor.putString(PREF_ICONS_SET, set); editor.apply(); } - public int getIconsSet() + public IconSet getIconsSet() { - return getPrefs().getInt(PREF_ICONS_SET, ICONS_SET_SEASON); - + return getPrefs().getInt(PREF_ICONS_SET, IconSetList.getDefault()); } public void reset() diff --git a/src/main/java/org/androidsoft/games/memory/kids/ui/MainActivity.java b/src/main/java/org/androidsoft/games/memory/kids/ui/MainActivity.java index 94fa6d9..dea78a5 100644 --- a/src/main/java/org/androidsoft/games/memory/kids/ui/MainActivity.java +++ b/src/main/java/org/androidsoft/games/memory/kids/ui/MainActivity.java @@ -20,6 +20,8 @@ import android.view.View; import java.text.MessageFormat; import org.androidsoft.games.memory.kids.PreferencesService; +import org.androidsoft.games.memory.kids.IconSet; +import org.androidsoft.games.memory.kids.IconSetList; import org.androidsoft.games.memory.kids.R; /** @@ -28,55 +30,6 @@ */ public class MainActivity extends AbstractMainActivity implements Memory.OnMemoryListener { - - private static final int[] tiles_default = - { - R.drawable.default_1, R.drawable.default_2, - R.drawable.default_3, R.drawable.default_4, R.drawable.default_5, R.drawable.default_6, - R.drawable.default_7, R.drawable.default_8, R.drawable.default_9, R.drawable.default_10, - R.drawable.default_11, R.drawable.default_12, R.drawable.default_13, R.drawable.default_14, - R.drawable.default_15, R.drawable.default_16, R.drawable.default_17, R.drawable.default_18, - R.drawable.default_19, R.drawable.default_20, R.drawable.default_21, R.drawable.default_22, - R.drawable.default_23, R.drawable.default_24, R.drawable.default_25, R.drawable.default_26, - R.drawable.default_27, R.drawable.default_28, R.drawable.default_29, R.drawable.default_30, - R.drawable.default_31, R.drawable.default_32, R.drawable.default_33, R.drawable.default_34 - }; - - - private static final int[] tiles_christmas = - { - R.drawable.christmas_1, R.drawable.christmas_2, - R.drawable.christmas_3, R.drawable.christmas_4, R.drawable.christmas_5, R.drawable.christmas_6, - R.drawable.christmas_7, R.drawable.christmas_8, R.drawable.christmas_9, R.drawable.christmas_10, - R.drawable.christmas_11, R.drawable.christmas_12, R.drawable.christmas_13, R.drawable.christmas_14 , - R.drawable.christmas_15, R.drawable.christmas_16, R.drawable.christmas_17, R.drawable.christmas_18, - R.drawable.christmas_19, R.drawable.christmas_20, R.drawable.christmas_21, R.drawable.christmas_22 - }; - - - private static final int[] tiles_easter = - { - R.drawable.easter_1, R.drawable.easter_2, - R.drawable.easter_3, R.drawable.easter_4, R.drawable.easter_5, R.drawable.easter_6, - R.drawable.easter_7, R.drawable.easter_8, R.drawable.easter_9, R.drawable.easter_10, - R.drawable.easter_11, R.drawable.easter_12, R.drawable.easter_13, R.drawable.easter_14 - }; - - private static final int[] tiles_tux = - { - R.drawable.tux_1, R.drawable.tux_2, - R.drawable.tux_3, R.drawable.tux_4, R.drawable.tux_5, R.drawable.tux_6, - R.drawable.tux_7, R.drawable.tux_8, R.drawable.tux_9, R.drawable.tux_10, - R.drawable.tux_11, R.drawable.tux_12, R.drawable.tux_13, R.drawable.tux_14, - R.drawable.tux_15, R.drawable.tux_16, R.drawable.tux_17, R.drawable.tux_18, - R.drawable.tux_19, R.drawable.tux_20, R.drawable.tux_21, R.drawable.tux_22, - R.drawable.tux_23, R.drawable.tux_24, R.drawable.tux_25, R.drawable.tux_26, - R.drawable.tux_27, R.drawable.tux_28, R.drawable.tux_29, R.drawable.tux_30, - R.drawable.tux_31, R.drawable.tux_32, R.drawable.tux_33 - }; - - private static final int[][] icons_set = { tiles_default , tiles_christmas, tiles_easter, tiles_tux }; - private static final int[] sounds = { R.raw.blop, R.raw.chime, R.raw.chtoing, R.raw.tic, R.raw.toc, R.raw.toing, R.raw.toing2, R.raw.toing3, R.raw.toing4, R.raw.toing5, @@ -84,10 +37,6 @@ public class MainActivity extends AbstractMainActivity implements Memory.OnMemor }; - private static final int[] not_found_tile_set = - { - R.drawable.not_found_default, R.drawable.not_found_christmas, R.drawable.not_found_easter, R.drawable.not_found_tux - }; private Memory mMemory; private MemoryGridView mGridView; @@ -118,8 +67,8 @@ protected View getGameView() @Override protected void newGame() { - int set = PreferencesService.instance().getIconsSet(); - mMemory = new Memory( icons_set[ set ], sounds , not_found_tile_set[ set ], this); + IconSet is = PreferencesService.getIconsSet(); + mMemory = new Memory((int[])(is.getImages()), sounds , (int)(is.getVerso()), this); mMemory.reset(); mGridView = (MemoryGridView) findViewById(R.id.gridview); mGridView.setMemory(mMemory); diff --git a/src/main/java/org/androidsoft/games/memory/kids/ui/PreferencesActivity.java b/src/main/java/org/androidsoft/games/memory/kids/ui/PreferencesActivity.java index a4ff83b..c26a7f5 100644 --- a/src/main/java/org/androidsoft/games/memory/kids/ui/PreferencesActivity.java +++ b/src/main/java/org/androidsoft/games/memory/kids/ui/PreferencesActivity.java @@ -37,7 +37,7 @@ public class PreferencesActivity extends BasicActivity implements OnClickListene private Button mButtonSupport; private CompoundButton mCbSoundEnabled; private Spinner mSpinner; - private int mIconSet; + private String mIconSet; /** * {@inheritDoc } @@ -57,7 +57,7 @@ public void onCreate(Bundle icicle) mSpinner = (Spinner) findViewById(R.id.spinner_theme); - mIconSet = PreferencesService.instance().getIconsSet(); + mIconSet = PreferencesService.instance().getIconsSet().hashCode(); mSpinner.setSelection( mIconSet ); mSpinner.setOnItemSelectedListener(new OnItemSelectedListener()