diff --git a/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/Bookmark.java b/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/Bookmark.java index e177a1c..c795b02 100644 --- a/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/Bookmark.java +++ b/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/Bookmark.java @@ -5,12 +5,20 @@ import android.content.Context; import android.content.DialogInterface; import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.util.Log; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Toast; import com.gmail.afonsotrepa.pocketgopher.gopherclient.Page; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; @@ -26,7 +34,7 @@ public class Bookmark public Integer id; //a unique id that identifies the bookmark - private static final Integer BOOKMARKS_FILE = R.string.booksmarks_file; + private static final String BOOKMARKS_FILE = "bookmarks"; private Bookmark(String name, String url, Integer id) { @@ -43,12 +51,12 @@ public Bookmark(Context context, String name, String url) this.name = name; //generate a new unique id - String file = context.getResources().getString(BOOKMARKS_FILE); - SharedPreferences sharedPref = context.getSharedPreferences(file, Context.MODE_PRIVATE); - id = sharedPref.getInt("id", 0); + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences + (context); + id = sharedPreferences.getInt("id", 0); - //update the (id part in the) file - SharedPreferences.Editor editor = sharedPref.edit(); + //update the file + SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("id", id + 1); editor.apply(); } @@ -68,67 +76,92 @@ public Bookmark(Context context, String name, Character type, String selector, S /** - * Save bookmarks to a SharedPreferences file + * Add a bookmark to the file */ - static void save(Context context, List bookmarks) + void add(Context context) { - String file = context.getResources().getString(BOOKMARKS_FILE); - //open/create the file in private mode - SharedPreferences sharedPref = context.getSharedPreferences(file, Context.MODE_PRIVATE); - SharedPreferences.Editor editor = sharedPref.edit(); - - //transform the list into a StringBuilder - StringBuilder csvbookmarks = new StringBuilder(); - for (Bookmark b : bookmarks) + try { - csvbookmarks.append(b.name).append("\n"); - csvbookmarks.append(b.url).append("\n"); - csvbookmarks.append(b.id.toString()).append("\n"); - csvbookmarks.append("\u0000"); + FileOutputStream outputStream = context.openFileOutput(BOOKMARKS_FILE, + Context.MODE_APPEND + ); + + outputStream.write(( + this.name + "\t" + + this.url + "\t" + + this.id.toString() + "\n" + ).getBytes()); + outputStream.close(); + } + catch (IOException e) + { + throw new RuntimeException(e); } - - //write csvbookmarks to the editor - editor.putString("bookmarks", csvbookmarks.toString()); - //apply the changes to the file - editor.apply(); } /** - * Read the bookmarks from a SharedPreferences file + * Read the bookmarks from the bookmarks file * * @return a list of all the bookmarks in the bookmarks file */ - static List read(Context context) throws Exception + static List read(Context context) { - String file = context.getResources().getString(BOOKMARKS_FILE); - //open/create the file in private mode - SharedPreferences sharedPref = context.getSharedPreferences(file, Context.MODE_PRIVATE); - + try + { + FileInputStream inputStream = context.openFileInput(BOOKMARKS_FILE); + BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputStream)); - List bookmarks = new ArrayList<>(); + //read the bookmark(s) from the file + List bookmarks = new ArrayList<>(); + String b; + while ((b = bufferedreader.readLine()) != null) + { + String[] bsplit = b.split("\t"); + if (bsplit.length > 1) + { + //parse the bookmark + Bookmark bookmark = new Bookmark( + bsplit[0], //name + bsplit[1], //url + Integer.parseInt(bsplit[2]) //id + ); + + //add it to the list of bookmarks + bookmarks.add(bookmark); + } + } - //read the bookmark(s) from the file - String[] csvbookmarks = sharedPref.getString("bookmarks", "").split("\u0000"); + return bookmarks; + } + catch (IOException e) + { + return null; + } + } - for (String b : csvbookmarks) + public void remove(Context context) + { + try { - String[] bsplit = b.split("\n"); - if (bsplit.length > 1) + List bookmarks = Bookmark.read(context); + + //clear the file + context.openFileOutput(BOOKMARKS_FILE, Context.MODE_PRIVATE ).write("".getBytes()); + + for (Bookmark b : bookmarks) { - //parse the bookmark - Bookmark bookmark = new Bookmark( - bsplit[0], //name - bsplit[1], //url - Integer.parseInt(bsplit[2]) - ); //id - - //add it to the list of bookmarks - bookmarks.add(bookmark); + if (b.id != this.id) + { + b.add(context); + } } - } - return bookmarks; + } + catch (Exception e) + { + throw new RuntimeException(e); + } } @@ -163,6 +196,7 @@ public void editBookmark(final Context context) //apply the layout dialog.setView(layout); + final Bookmark bookmark = this; dialog.setPositiveButton("Save", new DialogInterface.OnClickListener() @@ -172,35 +206,11 @@ public void onClick(final DialogInterface dialog, int which) { try { - //list of current bookmarks - List bookmarks = Bookmark.read(context); - - //if editing a bookmark that already exists - if (id != 0) - { - //remove the old bookmark - for (Bookmark b : bookmarks) - { - if (b.id.equals(id)) - { - bookmarks.remove(b); - } - } - } - //create the new bookmark - Bookmark b = new Bookmark( - context, - editName.getText().toString(), - editUrl.getText().toString() - ); - - //add it to the list - bookmarks.add(b); - - //save the changes to the file - Bookmark.save(context, bookmarks); - - //show "Bookmark saved" and exit this activity + bookmark.name = editName.getText().toString(); + bookmark.url = editUrl.getText().toString(); + + bookmark.add(context); + Toast.makeText(context, "Bookmark saved", Toast.LENGTH_SHORT) .show(); dialog.cancel(); @@ -230,20 +240,7 @@ public void onClick(DialogInterface dialog, int which) { try { - List bookmarks = Bookmark.read(context); - - //remove the bookmark from bookmarks (if it exists) - for (Bookmark bookmark : bookmarks) - { - if (bookmark.id.equals(id)) - { - bookmarks.remove(bookmark); - break; - } - } - - //save the edited bookmarks list - Bookmark.save(context, bookmarks); + bookmark.remove(context); dialog.cancel(); } diff --git a/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/History.java b/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/History.java index 5e909f4..1f6ff5d 100644 --- a/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/History.java +++ b/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/History.java @@ -37,7 +37,8 @@ static public void add(Context context, String url) Context.MODE_APPEND ); - outputStream.write((url + "\n").getBytes()); + outputStream.write(url.getBytes()); + outputStream.write("\n".getBytes()); outputStream.close(); } catch (IOException e) diff --git a/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/MainActivity.java b/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/MainActivity.java index a4aeb87..f16f100 100644 --- a/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/MainActivity.java +++ b/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/MainActivity.java @@ -1,14 +1,15 @@ package com.gmail.afonsotrepa.pocketgopher; import android.app.AlertDialog; -import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.design.widget.FloatingActionButton; import android.support.v7.app.AppCompatActivity; import android.text.InputType; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -16,7 +17,6 @@ import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; -import android.widget.Toast; import com.gmail.afonsotrepa.pocketgopher.gopherclient.Page; @@ -28,8 +28,8 @@ public class MainActivity extends AppCompatActivity private Menu menu; public static int font = R.style.monospace; - private static final Integer SETTINGS_FILE = R.string.settings_file; private static final String MONOSPACE_FONT_SETTING = "monospace_font"; + private static final String FIRST_RUN = "first_run"; @Override protected void onCreate(Bundle savedInstanceState) @@ -37,10 +37,9 @@ protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - String file = this.getResources().getString(SETTINGS_FILE); - //open/create the file in private mode - SharedPreferences sharedPref = this.getSharedPreferences(file, Context.MODE_PRIVATE); - if (sharedPref.getInt(MONOSPACE_FONT_SETTING, 1) == 1) + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + SharedPreferences.Editor editor = sharedPreferences.edit(); + if (sharedPreferences.getInt(MONOSPACE_FONT_SETTING, 1) == 1) { font = R.style.monospace; } @@ -48,6 +47,17 @@ protected void onCreate(Bundle savedInstanceState) { font = R.style.serif; } + + if (sharedPreferences.getBoolean(FIRST_RUN, true)) + { + editor.putBoolean(FIRST_RUN, false); + editor.apply(); + + new Bookmark(this, "Search Veronica-2", "gopher.floodgap.com/1/v2") + .add(this); + new Bookmark(this, "SDF", "sdf.org").add(this); + new Bookmark(this, "Khzae", "khzae.net").add(this); + } } @Override @@ -55,17 +65,24 @@ public void onResume() { super.onResume(); - List bookmarks; - - try + //configure the add bookmark button + FloatingActionButton addBookmarkFloatingButton = findViewById(R.id + .addBookmarkFloatingButton); + addBookmarkFloatingButton.setOnClickListener(new View.OnClickListener() { - bookmarks = Bookmark.read(this); - } - catch (Exception e) + @Override + public void onClick(View v) + { + //make the new bookmark + Bookmark.makeBookmark(MainActivity.this); + } + }); + + + List bookmarks = Bookmark.read(MainActivity.this); + + if (bookmarks == null) { - e.printStackTrace(); - //display the error and return - Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); return; } @@ -120,20 +137,6 @@ public boolean onItemLongClick(AdapterView parent, View view, int position, l return true; } }); - - - //configure the add bookmark button - FloatingActionButton addBookmarkFloatingButton = findViewById(R.id - .addBookmarkFloatingButton); - addBookmarkFloatingButton.setOnClickListener(new View.OnClickListener() - { - @Override - public void onClick(View v) - { - //make the new bookmark - Bookmark.makeBookmark(MainActivity.this); - } - }); } @@ -146,12 +149,10 @@ public boolean onCreateOptionsMenu(Menu menu) this.menu = menu; menu.findItem(R.id.monospace_font).setChecked(true); - String file = this.getResources().getString(SETTINGS_FILE); - //open/create the file in private mode - SharedPreferences sharedPref = this.getSharedPreferences(file, Context.MODE_PRIVATE); - SharedPreferences.Editor editor = sharedPref.edit(); + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + SharedPreferences.Editor editor = sharedPreferences.edit(); - if (sharedPref.getInt(MONOSPACE_FONT_SETTING, 1) == 1) + if (sharedPreferences.getInt(MONOSPACE_FONT_SETTING, 1) == 1) { menu.findItem(R.id.monospace_font).setChecked(true); } @@ -170,11 +171,9 @@ public boolean onOptionsItemSelected(MenuItem item) switch (item.getItemId()) { case R.id.monospace_font: - String file = this.getResources().getString(SETTINGS_FILE); - //open/create the file in private mode - SharedPreferences sharedPref = this.getSharedPreferences(file, Context - .MODE_PRIVATE); - SharedPreferences.Editor editor = sharedPref.edit(); + SharedPreferences sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(this); + SharedPreferences.Editor editor = sharedPreferences.edit(); if (font == R.style.serif) { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b3a1190..711fbee 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -14,7 +14,6 @@ android:layout_marginBottom="16dp" android:layout_marginRight="16dp" android:tint="@android:color/white" - android:background="@android:color/background_dark" android:clickable="true" app:layout_constraintBottom_toBottomOf="parent"