Android Open Source - 101AndroidApps Wiki






From Project

Back to project page 101AndroidApps.

License

The source code is released under:

Licenced under the Creative Commons Attribution 4.0 licence. For full text see http://creativecommons.org/licenses/by/4.0/

If you think the Android project 101AndroidApps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.hulzenga.ioi.android.app_007;
//from   ww  w  .  j  a  va2 s .co m
import android.content.Context;
import android.util.JsonReader;
import android.util.JsonWriter;
import android.util.Log;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Wiki {

  private static final String TAG = "WIKI";

  private static final String KEY_NAME    = "name";
  private static final String KEY_ADDRESS = "address";
  private static final String KEY_LINKS   = "links";
  private static final String KEY_CORRECT = "correct";

  private String       mName;
  private String       mAddress;
  private List<String> mLinks;
  private boolean      mCorrect;

  public Wiki(String name, String adress, List<String> links) {
    this(name, adress, links, false);
  }

  public Wiki(String name, String address, List<String> links, boolean correct) {
    mName = name;
    mAddress = address;
    mLinks = links;
    mCorrect = correct;
  }

  public String getName() {
    return mName;
  }

  public String getAddress() {
    return mAddress;
  }

  public List<String> getLinks() {
    return mLinks;
  }

  public Wiki stripLinks() {
    mLinks.clear();
    return this;
  }

  public boolean isCorrect() {
    return mCorrect;
  }

  public void setCorrect(boolean correct) {
    mCorrect = correct;
  }

  public static List<Wiki> loadWikis(Context context, String fileName, boolean deleteAfterReading) {
    List<Wiki> wikiList = new ArrayList<Wiki>();

    File saveFile = new File(context.getFilesDir(), fileName + ".json");
    // if there is no save file return empty list
    if (!saveFile.exists()) {
      return wikiList;
    }

    // try and read wikis from the list
    try {
      FileReader fileReader = new FileReader(saveFile);
      JsonReader jsonReader = new JsonReader(fileReader);

      jsonReader.beginArray();
      while (jsonReader.hasNext()) {
        wikiList.add(readWiki(jsonReader));
      }
      jsonReader.endArray();

      jsonReader.close();
    } catch (IOException e) {
      // if write fails, log it. Nothing else necessary
      Log.e(TAG, "failed to write wikis to file: " + e.getMessage());
    } catch (IllegalStateException e) {
      Log.e(TAG, "JSON reader state failure: " + e.getMessage());
    } finally {
      if (deleteAfterReading) {
        saveFile.delete();
      }
    }

    return wikiList;
  }

  private static Wiki readWiki(JsonReader reader) throws IOException {
    String wikiName = null;
    String adress = null;
    List<String> links = new ArrayList<String>();
    boolean correct = false;

    reader.beginObject();
    while (reader.hasNext()) {
      String name = reader.nextName();

      if (name.equals(KEY_NAME)) {
        wikiName = reader.nextString();
      } else if (name.equals(KEY_ADDRESS)) {
        adress = reader.nextString();
      } else if (name.equals(KEY_LINKS)) {
        reader.beginArray();
        while (reader.hasNext()) {
          links.add(reader.nextString());
        }
        reader.endArray();
      } else if (name.equals(KEY_CORRECT)) {
        correct = reader.nextBoolean();
      } else {
        reader.skipValue();
      }
    }
    reader.endObject();

    return new Wiki(wikiName, adress, links, correct);
  }

  public static void saveWikis(Context context, List<Wiki> wikis, String fileName) {
    File saveFile = new File(context.getFilesDir(), fileName + ".json");
    boolean succes = false;
    try {
      FileWriter fileWriter = new FileWriter(saveFile);
      JsonWriter jsonWriter = new JsonWriter(fileWriter);
      jsonWriter.beginArray();
      for (Wiki wiki : wikis) {
        wiki.writeWiki(jsonWriter);
      }
      jsonWriter.endArray();
      jsonWriter.close();
      succes = true;
    } catch (IOException e) {
      // if write fails, log it. Nothing else necessary
      Log.e(TAG, "failed to write wikis in buffer to file");
    } finally {
      if (!succes) {
        saveFile.delete();
      }
    }
  }

  private void writeWiki(JsonWriter writer) throws IOException {
    writer.beginObject();
    writer.name(KEY_NAME).value(mName);
    writer.name(KEY_ADDRESS).value(mAddress);

    writer.name(KEY_LINKS);
    writer.beginArray();
    for (String link : mLinks) {
      writer.value(link);
    }
    writer.endArray();

    writer.name(KEY_CORRECT).value(mCorrect);
    writer.endObject();
  }
}




Java Source Code List

com.hulzenga.ioi.android.AppActivity.java
com.hulzenga.ioi.android.AppDetailsDialog.java
com.hulzenga.ioi.android.App.java
com.hulzenga.ioi.android.TableOfContentsActivity.java
com.hulzenga.ioi.android.app_001.HelloWorldActivity.java
com.hulzenga.ioi.android.app_002.Ball.java
com.hulzenga.ioi.android.app_002.BouncyBallsActivity.java
com.hulzenga.ioi.android.app_002.BouncyBallsView.java
com.hulzenga.ioi.android.app_003.MonsterDatabaseActivity.java
com.hulzenga.ioi.android.app_003.MonsterEditDialog.java
com.hulzenga.ioi.android.app_003.MonsterGenerator.java
com.hulzenga.ioi.android.app_003.MonsterListAdapter.java
com.hulzenga.ioi.android.app_003.database.MonsterContract.java
com.hulzenga.ioi.android.app_003.database.MonsterDatabaseHelper.java
com.hulzenga.ioi.android.app_003.database.MonsterProvider.java
com.hulzenga.ioi.android.app_004.BouncyBall3dActivity.java
com.hulzenga.ioi.android.app_004.BouncyBall3dGLSurfaceView.java
com.hulzenga.ioi.android.app_004.BouncyBall3dRenderer.java
com.hulzenga.ioi.android.app_004.BouncyBallGame.java
com.hulzenga.ioi.android.app_005.ElementActivity.java
com.hulzenga.ioi.android.app_005.ElementAdapter.java
com.hulzenga.ioi.android.app_005.ElementAnimator.java
com.hulzenga.ioi.android.app_005.ElementSnakeView.java
com.hulzenga.ioi.android.app_005.ElementView.java
com.hulzenga.ioi.android.app_005.Element.java
com.hulzenga.ioi.android.app_006.EmptyFragment.java
com.hulzenga.ioi.android.app_006.ExposureFragment.java
com.hulzenga.ioi.android.app_006.SettingChangeListener.java
com.hulzenga.ioi.android.app_006.SettingIconFragment.java
com.hulzenga.ioi.android.app_006.SettingMenuFragment.java
com.hulzenga.ioi.android.app_006.SettingMenuLvl2Fragment.java
com.hulzenga.ioi.android.app_006.Setting.java
com.hulzenga.ioi.android.app_006.SimpleCameraActivity.java
com.hulzenga.ioi.android.app_006.SimpleCameraPreview.java
com.hulzenga.ioi.android.app_007.ButtonsFragment.java
com.hulzenga.ioi.android.app_007.HighScores.java
com.hulzenga.ioi.android.app_007.LinksFragment.java
com.hulzenga.ioi.android.app_007.Menu.java
com.hulzenga.ioi.android.app_007.Review.java
com.hulzenga.ioi.android.app_007.Score.java
com.hulzenga.ioi.android.app_007.StatusFragment.java
com.hulzenga.ioi.android.app_007.WikiGameActivity.java
com.hulzenga.ioi.android.app_007.Wiki.java
com.hulzenga.ioi.android.app_008.OverheidActivity.java
com.hulzenga.ioi.android.app_008.OverheidFeedAdapter.java
com.hulzenga.ioi.android.app_008.OverheidFeedFragment.java
com.hulzenga.ioi.android.app_008.OverheidFeedItem.java
com.hulzenga.ioi.android.app_008.OverheidFeed.java
com.hulzenga.ioi.android.app_008.OverheidPagerAdapter.java
com.hulzenga.ioi.android.app_009.ThumbsUpActivity.java
com.hulzenga.ioi.android.app_009.ThumbsUpServer.java
com.hulzenga.ioi.android.app_009.ThumbsUpService.java
com.hulzenga.ioi.android.app_010.CompassActivity.java
com.hulzenga.ioi.android.app_010.CompassDetailsView.java
com.hulzenga.ioi.android.app_010.CompassView.java
com.hulzenga.ioi.android.util.Constrain.java
com.hulzenga.ioi.android.util.DeveloperTools.java
com.hulzenga.ioi.android.util.FileManager.java
com.hulzenga.ioi.android.util.ScreenMetrics.java
com.hulzenga.ioi.android.util.open_gl.ColorFunctionFactory.java
com.hulzenga.ioi.android.util.open_gl.ColorFunction.java
com.hulzenga.ioi.android.util.open_gl.ShaderTools.java
com.hulzenga.ioi.android.util.open_gl.engine.NodeController.java
com.hulzenga.ioi.android.util.open_gl.engine.SceneGraph.java
com.hulzenga.ioi.android.util.open_gl.engine.SceneNode.java
com.hulzenga.ioi.android.util.open_gl.geometry.Box.java
com.hulzenga.ioi.android.util.open_gl.geometry.Cylinder.java
com.hulzenga.ioi.android.util.open_gl.geometry.Geometry.java
com.hulzenga.ioi.android.util.open_gl.geometry.Grid.java
com.hulzenga.ioi.android.util.open_gl.geometry.Sphere.java
com.hulzenga.ioi.android.util.open_gl.geometry.Transform.java
com.hulzenga.ioi.android.util.open_gl.vector.Vec3.java
com.hulzenga.ioi.android.util.open_gl.vector.Vec4.java
fi.iki.elonen.NanoHTTPD.java