Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions local.properties

This file was deleted.

79 changes: 79 additions & 0 deletions src/main/java/org/androidsoft/games/memory/kids/IconSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* Copyright (c) 2017 Raphaël Droz <raphael.droz@gmail.com>
* 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 <http://www.gnu.org/licenses/>.
*/

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<BitmapDrawable> images = new ArrayList<BitmapDrawable>();


// constructor from filepath/URL
// http://stackoverflow.com/a/9490060
public IconSet(String title, String verso_path, List<String> 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<BitmapDrawable>(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<BitmapDrawable>)new ArrayList<Int>(images);
}

public List<BitmapDrawable> getImages() {
return images;
}

public BitmapDrawable getVerso() {
return verso;
}

public String getTitle() {
return title;
}
}
158 changes: 158 additions & 0 deletions src/main/java/org/androidsoft/games/memory/kids/IconSetList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/* Copyright (c) 2017 Raphaël Droz <raphael.droz@gmail.com>
* 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 <http://www.gnu.org/licenses/>.
*/

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<String, IconSet> set;
private static HashMap<String, IconSet> 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<String, IconSet> aset = new HashMap<String, IconSet>();
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<String> img = new ArrayList<String>();

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*/) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -28,66 +30,13 @@
*/
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,
R.raw.toing6, R.raw.toong, R.raw.tzirlup, R.raw.whiipz
};


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;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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()
Expand Down