From 90ee611af8ba4c38e70f27eb0df55e9134ad625c Mon Sep 17 00:00:00 2001 From: Renard Date: Mon, 11 Apr 2016 17:50:06 -0300 Subject: New ViewActivity, ThemeManager --- .../main/java/layout/FragmentBoardItemList.java | 10 ++- app/src/main/java/layout/FragmentImage.java | 18 ++++- .../baiparser/LicensesActivity.java | 5 +- .../baiparser/MainActivity.java | 48 ++++------- .../baiparser/ResponseActivity.java | 6 +- .../baiparser/SettingsActivity.java | 2 + .../baiparser/ThemeManager.java | 92 +++++++++++++++++++--- .../baiparser/ThreadListAdapter.java | 9 ++- .../baiparser/ViewerActivity.java | 8 +- app/src/main/res/layout/activity_response.xml | 1 - .../main/res/layout/fragment_fragment_image.xml | 33 +++++++- .../res/layout/fragment_fragment_thread_list.xml | 1 - app/src/main/res/menu/menu_viewer.xml | 3 +- app/src/main/res/values/colors.xml | 4 +- app/src/main/res/values/drawables.xml | 2 +- app/src/main/res/values/styles.xml | 14 +++- 16 files changed, 183 insertions(+), 73 deletions(-) diff --git a/app/src/main/java/layout/FragmentBoardItemList.java b/app/src/main/java/layout/FragmentBoardItemList.java index 38e4819..149def3 100644 --- a/app/src/main/java/layout/FragmentBoardItemList.java +++ b/app/src/main/java/layout/FragmentBoardItemList.java @@ -38,6 +38,7 @@ import org.bienvenidoainternet.baiparser.MainActivity; import org.bienvenidoainternet.baiparser.R; import org.bienvenidoainternet.baiparser.RecentPostAdapter; import org.bienvenidoainternet.baiparser.ResponseActivity; +import org.bienvenidoainternet.baiparser.ThemeManager; import org.bienvenidoainternet.baiparser.ThreadListAdapter; import org.bienvenidoainternet.baiparser.structure.Board; import org.bienvenidoainternet.baiparser.structure.BoardItem; @@ -80,6 +81,8 @@ public class FragmentBoardItemList extends Fragment { LinearLayout layoutThreadProcess; TextView txtThreadProcess; + ThemeManager tm; + public FragmentBoardItemList() { // Required empty public constructor @@ -104,6 +107,7 @@ public class FragmentBoardItemList extends Fragment { this.currentThread = getArguments().getParcelable(ARG_CURRENT_THREAD); this.imMainFragment = getArguments().getBoolean(ARG_MAIN_FRAGMENT); } + tm = new ThemeManager(getActivity()); } @Override @@ -130,7 +134,7 @@ public class FragmentBoardItemList extends Fragment { // AplicaciĆ³n del Tema settings = PreferenceManager.getDefaultSharedPreferences(this.getContext()); - int themeResId = ((MainActivity)getActivity()).getCurrentThemeId(); + int themeResId = tm.getCurrentThemeId(); Context context = new ContextThemeWrapper(getActivity(), themeResId); LayoutInflater localInflater = inflater.cloneInContext(context); View v = localInflater.inflate(R.layout.fragment_fragment_thread_list, container, false); @@ -145,14 +149,14 @@ public class FragmentBoardItemList extends Fragment { this.loadingBar = (ProgressBar)rootView.findViewById(R.id.progressBar); // Agregamos color al divider del listview - ColorDrawable cd = new ColorDrawable((((MainActivity) getActivity()).themeManager).getMarginColor()); + ColorDrawable cd = new ColorDrawable(tm.getMarginColor()); listViewBoardItems.setDivider(cd); listViewBoardItems.setDividerHeight(1); // registramos los menus del listview registerForContextMenu(listViewBoardItems); // Creamos los dos adaptadores y los seteamos dependiendo del modo del fragmento - listViewAdapter = new ThreadListAdapter(v.getContext(), boardItems, (((MainActivity) getActivity()).themeManager)); + listViewAdapter = new ThreadListAdapter(v.getContext(), boardItems, tm); recentPostAdapter = new RecentPostAdapter(v.getContext(), boardItems); if (recentPostMode){ listViewBoardItems.setAdapter(recentPostAdapter); diff --git a/app/src/main/java/layout/FragmentImage.java b/app/src/main/java/layout/FragmentImage.java index 399e9f3..f337e5f 100644 --- a/app/src/main/java/layout/FragmentImage.java +++ b/app/src/main/java/layout/FragmentImage.java @@ -2,6 +2,7 @@ package layout; import android.content.Context; import android.content.ContextWrapper; +import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; @@ -9,8 +10,9 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; import android.widget.ProgressBar; -import android.widget.TextView; +import android.widget.RelativeLayout; import com.davemorrissey.labs.subscaleview.ImageSource; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; @@ -19,7 +21,6 @@ import com.koushikdutta.ion.Ion; import org.bienvenidoainternet.baiparser.R; import org.bienvenidoainternet.baiparser.ViewerActivity; -import org.bienvenidoainternet.baiparser.structure.BoardItem; import org.bienvenidoainternet.baiparser.structure.BoardItemFile; import java.io.File; @@ -80,11 +81,24 @@ public class FragmentImage extends Fragment { gifView = (GifImageView) view.findViewById(R.id.gifView); imageView.setVisibility(View.GONE); gifView.setVisibility(View.GONE); + RelativeLayout layoutOpenBrowser = (RelativeLayout) view.findViewById(R.id.layoutOpenBrowser); if (boardItemFile.file != null) { if (!boardItemFile.file.endsWith(".webm") && !boardItemFile.file.endsWith(".swf")) { + layoutOpenBrowser.setVisibility(View.GONE); downloadFile(); + }else{ + layoutOpenBrowser.setVisibility(View.VISIBLE); } } + Button btnOpenBrowser = (Button) view.findViewById(R.id.btnLaunchBrowser); + btnOpenBrowser.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(boardItemFile.fileURL)); + in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + v.getContext().startActivity(in); + } + }); return view; } diff --git a/app/src/main/java/org/bienvenidoainternet/baiparser/LicensesActivity.java b/app/src/main/java/org/bienvenidoainternet/baiparser/LicensesActivity.java index b91ce2b..575754a 100644 --- a/app/src/main/java/org/bienvenidoainternet/baiparser/LicensesActivity.java +++ b/app/src/main/java/org/bienvenidoainternet/baiparser/LicensesActivity.java @@ -1,7 +1,7 @@ package org.bienvenidoainternet.baiparser; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; import android.webkit.WebView; public class LicensesActivity extends AppCompatActivity { @@ -9,7 +9,10 @@ public class LicensesActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + ThemeManager tm = new ThemeManager(this); + this.setTheme(tm.getThemeForActivity()); setContentView(R.layout.activity_licenses); + getSupportActionBar().setTitle("Acerca de BaI App"); WebView webView = (WebView) findViewById(R.id.webView); webView.loadUrl("file:///android_asset/html/licenses.html"); } diff --git a/app/src/main/java/org/bienvenidoainternet/baiparser/MainActivity.java b/app/src/main/java/org/bienvenidoainternet/baiparser/MainActivity.java index 9130160..18d2e08 100644 --- a/app/src/main/java/org/bienvenidoainternet/baiparser/MainActivity.java +++ b/app/src/main/java/org/bienvenidoainternet/baiparser/MainActivity.java @@ -31,6 +31,7 @@ import com.koushikdutta.ion.Ion; import org.bienvenidoainternet.baiparser.structure.Board; import org.bienvenidoainternet.baiparser.structure.BoardItem; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -78,46 +79,25 @@ public class MainActivity extends AppCompatActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + ThemeManager tm = new ThemeManager(this); + this.setTheme(tm.getThemeForMainActivity()); if (savedInstanceState != null) { currentThemeId = savedInstanceState.getInt("currentThemeId"); boardList = savedInstanceState.getParcelableArrayList("boardList"); } SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); - themeId = Integer.valueOf(settings.getString("pref_theme", "1")); - if (settings.getString("pref_password", "").isEmpty()){ SharedPreferences.Editor edit = settings.edit(); edit.putString("pref_password", makePassword()); edit.commit(); } - switch (themeId) { - case 1: - currentThemeId = R.style.AppTheme_NoActionBar; - break; - case 2: - currentThemeId = R.style.AppTheme_Dark; - break; - case 3: - currentThemeId = R.style.AppTheme_HeadLine; - setTheme(R.style.AppTheme_HeadLine_Activity); - break; - case 4: - currentThemeId = R.style.AppTheme_Black; - setTheme(R.style.AppTheme_Black_Activity); - break; - } - - themeManager = new ThemeManager(this); - Log.d("ThemeManager", "isDarkTheme: " + themeManager.isDarkTheme()); - setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle("Bievenido a internet"); this.setSupportActionBar(toolbar); - fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override @@ -372,25 +352,25 @@ public class MainActivity extends AppCompatActivity final SubMenu sub = menu.addSubMenu("Lista de Boards"); Ion.with(getApplicationContext()) .load("http://bienvenidoainternet.org/cgi/api/boards") - .asJsonObject() - .setCallback(new FutureCallback() { + .asString() + .setCallback(new FutureCallback() { @Override - public void onCompleted(Exception e, JsonObject result) { + public void onCompleted(Exception e, String result) { if (e != null) { e.printStackTrace(); Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); - } else { - JsonArray boards = result.get("boards").getAsJsonArray(); - for (int i = 0; i < boards.size(); i++) { - try { - JSONObject board = new JSONObject(boards.get(i).toString()); + }else { + try { + JSONArray boards = new JSONObject(result).getJSONArray("boards"); + for (int i = 0; i < boards.length(); i++) { + JSONObject board = boards.getJSONObject(i); Board parsedBoard = new Board(board.getString("name"), board.getString("dir"), board.getInt("board_type")); sub.add(parsedBoard.getBoardName()); boardList.add(parsedBoard); - } catch (JSONException e1) { - e1.printStackTrace(); - Toast.makeText(getApplicationContext(), "Error parsing JSON", Toast.LENGTH_LONG).show(); } + }catch (JSONException e1) { + Toast.makeText(getApplicationContext(), e1.getMessage(), Toast.LENGTH_LONG).show(); + e1.printStackTrace(); } } } diff --git a/app/src/main/java/org/bienvenidoainternet/baiparser/ResponseActivity.java b/app/src/main/java/org/bienvenidoainternet/baiparser/ResponseActivity.java index e96868a..350de99 100644 --- a/app/src/main/java/org/bienvenidoainternet/baiparser/ResponseActivity.java +++ b/app/src/main/java/org/bienvenidoainternet/baiparser/ResponseActivity.java @@ -2,12 +2,10 @@ package org.bienvenidoainternet.baiparser; import android.content.Intent; import android.content.SharedPreferences; -import android.graphics.drawable.ColorDrawable; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -45,10 +43,12 @@ public class ResponseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + ThemeManager tm = new ThemeManager(this); + this.setTheme(tm.getThemeForActivity()); setContentView(R.layout.activity_response); + getSupportActionBar().setTitle("Respondiendo"); settings = PreferenceManager.getDefaultSharedPreferences(this); password = settings.getString("pref_password", "12345678"); - Log.v("password", password); if (savedInstanceState != null){ this.theReply = savedInstanceState.getParcelable("theReply"); diff --git a/app/src/main/java/org/bienvenidoainternet/baiparser/SettingsActivity.java b/app/src/main/java/org/bienvenidoainternet/baiparser/SettingsActivity.java index e8e7df4..eefc5fb 100644 --- a/app/src/main/java/org/bienvenidoainternet/baiparser/SettingsActivity.java +++ b/app/src/main/java/org/bienvenidoainternet/baiparser/SettingsActivity.java @@ -95,6 +95,8 @@ public class SettingsActivity extends AppCompatPreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + ThemeManager tm = new ThemeManager(this); + this.setTheme(tm.getThemeForActivity()); setupActionBar(); getFragmentManager().beginTransaction().replace(android.R.id.content, new GeneralPreferenceFragment()).commit(); } diff --git a/app/src/main/java/org/bienvenidoainternet/baiparser/ThemeManager.java b/app/src/main/java/org/bienvenidoainternet/baiparser/ThemeManager.java index 7f0be86..28c0d74 100644 --- a/app/src/main/java/org/bienvenidoainternet/baiparser/ThemeManager.java +++ b/app/src/main/java/org/bienvenidoainternet/baiparser/ThemeManager.java @@ -1,26 +1,54 @@ package org.bienvenidoainternet.baiparser; +import android.app.Activity; +import android.content.SharedPreferences; import android.content.res.TypedArray; import android.graphics.Color; +import android.preference.PreferenceManager; +import android.util.Log; /** * Created by Renard on 16-03-2016. */ public class ThemeManager { - private MainActivity ac; private int currentThemeId; - public ThemeManager(MainActivity ac){ - this.ac = ac; - this.currentThemeId = ac.getCurrentThemeId(); + private int prefThemeId; + private Activity activity; + public ThemeManager(Activity activity){ + this.activity = activity; + setCurrentThemeId(); + } + + public void setCurrentThemeId(){ + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity); + int themeId = Integer.valueOf(settings.getString("pref_theme", "1")); + prefThemeId = themeId; + switch (themeId) { + case 1: + currentThemeId = R.style.AppTheme_NoActionBar; + break; + case 2: + currentThemeId = R.style.AppTheme_Dark; + break; + case 3: + currentThemeId = R.style.AppTheme_HeadLine; +// setTheme(R.style.AppTheme_HeadLine_Activity); + break; + case 4: + currentThemeId = R.style.AppTheme_Black; +// setTheme(R.style.AppTheme_Black_Activity); + break; + } + Log.d("ThemeManager", "isDarkTheme: " + isDarkTheme()); } public int getSageColor(){ - TypedArray a = ac.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.sageColor}); + TypedArray a = activity.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.sageColor}); return a.getColor(0, Color.CYAN); } public int getMarginColor(){ - TypedArray a = ac.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.marginColor}); + TypedArray a = activity.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.marginColor}); return a.getColor(0, Color.CYAN); } @@ -29,27 +57,69 @@ public class ThemeManager { } public int getNameColor() { - TypedArray a = ac.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.nameColor}); + TypedArray a = activity.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.nameColor}); return a.getColor(0, Color.CYAN); } public int getTripcodeColor() { - TypedArray a = ac.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.tripcodeColor}); + TypedArray a = activity.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.tripcodeColor}); return a.getColor(0, Color.CYAN); } public int getPrimaryColor(){ - TypedArray a = ac.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.colorPrimary}); + TypedArray a = activity.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.colorPrimary}); return a.getColor(0, Color.CYAN); } public int getPrimaryDarkColor(){ - TypedArray a = ac.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.colorPrimaryDark}); + TypedArray a = activity.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.colorPrimaryDark}); return a.getColor(0, Color.CYAN); } public boolean isDarkTheme(){ - TypedArray a = ac.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.isDarkTheme}); + TypedArray a = activity.getTheme().obtainStyledAttributes(currentThemeId, new int[]{R.attr.isDarkTheme}); return a.getBoolean(0, false); } + public int getThemeForActivity(){ + int id = R.style.AppTheme; + switch (prefThemeId) { + case 1: // pseudoch + id = R.style.AppTheme; + break; + case 2: // nightmode + id = R.style.AppTheme; + break; + case 3: // photon + id = R.style.AppTheme_HeadLineActionBar; + break; + case 4: // tomorrow + id = R.style.AppTheme_BlackActionBar; + break; + } + return id; + } + + public int getThemeForMainActivity(){ + int id = R.style.AppTheme_NoActionBar; + switch (prefThemeId) { + case 1: // pseudoch + id = R.style.AppTheme_NoActionBar; + break; + case 2: // nightmode + id = R.style.AppTheme_NoActionBar; + break; + case 3: // photon + id = R.style.AppTheme_HeadLineActionBar_NoActionBar; + break; + case 4: // tomorrow + id = R.style.AppTheme_BlackActionBar_NoActionBar; + break; + } + return id; + } + + + public int getCurrentThemeId() { + return currentThemeId; + } } diff --git a/app/src/main/java/org/bienvenidoainternet/baiparser/ThreadListAdapter.java b/app/src/main/java/org/bienvenidoainternet/baiparser/ThreadListAdapter.java index 85cd891..7a85c1f 100644 --- a/app/src/main/java/org/bienvenidoainternet/baiparser/ThreadListAdapter.java +++ b/app/src/main/java/org/bienvenidoainternet/baiparser/ThreadListAdapter.java @@ -228,8 +228,15 @@ public class ThreadListAdapter extends ArrayAdapter{ txtReplies.setVisibility(boardItem.isReply ? View.GONE : View.VISIBLE); txtReplies.setText(boardItem.getTotalReplies() + " respuestas " + (boardItem.getTotalFiles() == 0 ? "" : ", " + boardItem.getTotalFiles() + " archivos")); + String fileExt = ""; + if (!boardItem.getFile().isEmpty()){ + String[] pathSplit = boardItem.getFile().split("\\."); + if (pathSplit.length != 0){ + fileExt = pathSplit[1].toUpperCase(); + } + } txtFileInfo.setVisibility(boardItem.getThumb().isEmpty() ? View.GONE : View.VISIBLE); - txtFileInfo.setText((boardItem.getFileSize() / 1024) + " KB " + boardItem.getThumbHeight() + "x" + boardItem.getThumbWidth()); + txtFileInfo.setText(fileExt + " " + (boardItem.getFileSize() / 1024) + " KB " + boardItem.getThumbHeight() + "x" + boardItem.getThumbWidth()); // Trasnparentar items con sage if (convertView != null){ diff --git a/app/src/main/java/org/bienvenidoainternet/baiparser/ViewerActivity.java b/app/src/main/java/org/bienvenidoainternet/baiparser/ViewerActivity.java index 9d140ec..8dc1262 100644 --- a/app/src/main/java/org/bienvenidoainternet/baiparser/ViewerActivity.java +++ b/app/src/main/java/org/bienvenidoainternet/baiparser/ViewerActivity.java @@ -14,9 +14,6 @@ import android.view.MenuItem; import android.widget.ProgressBar; import android.widget.Toast; -import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; - -import org.bienvenidoainternet.baiparser.structure.BoardItem; import org.bienvenidoainternet.baiparser.structure.BoardItemFile; import java.io.File; @@ -27,7 +24,6 @@ import java.io.OutputStream; import java.util.ArrayList; import layout.FragmentImage; -import pl.droidsonroids.gif.GifImageView; public class ViewerActivity extends AppCompatActivity implements FragmentImage.OnFragmentInteractionListener { private static final String EXTRA_FILELIST = "fileList", EXTRA_RELATIVEPOSITION = "position"; @@ -54,7 +50,6 @@ public class ViewerActivity extends AppCompatActivity implements FragmentImage.O pagerAdapter.addFragment(FragmentImage.newInstance(fileList.get(i))); } imagePager.setAdapter(pagerAdapter); - imagePager.setCurrentItem(relativePosition); imagePager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { @@ -63,7 +58,7 @@ public class ViewerActivity extends AppCompatActivity implements FragmentImage.O @Override public void onPageSelected(int position) { - getSupportActionBar().setSubtitle(fileList.get(position).file); + getSupportActionBar().setSubtitle("(" + (position + 1) + " / " + fileList.size() + ") " + fileList.get(position).file); } @Override @@ -71,6 +66,7 @@ public class ViewerActivity extends AppCompatActivity implements FragmentImage.O } }); + imagePager.setCurrentItem(relativePosition); this.getSupportActionBar().setDisplayHomeAsUpEnabled(true); } diff --git a/app/src/main/res/layout/activity_response.xml b/app/src/main/res/layout/activity_response.xml index bfd143e..15d35eb 100644 --- a/app/src/main/res/layout/activity_response.xml +++ b/app/src/main/res/layout/activity_response.xml @@ -1,7 +1,6 @@ + tools:context="layout.FragmentImage" + android:orientation="vertical"> + + + + + +