You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
249 lines
6.7 KiB
249 lines
6.7 KiB
9 years ago
|
package acr.browser.lightning;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
|
||
9 years ago
|
import acr.browser.lightning.reading.HtmlFetcher;
|
||
|
import acr.browser.lightning.reading.JResult;
|
||
9 years ago
|
import android.animation.ObjectAnimator;
|
||
9 years ago
|
import android.app.AlertDialog;
|
||
9 years ago
|
import android.app.ProgressDialog;
|
||
|
import android.content.Context;
|
||
9 years ago
|
import android.content.DialogInterface;
|
||
|
import android.content.DialogInterface.OnClickListener;
|
||
9 years ago
|
import android.content.Intent;
|
||
|
import android.os.AsyncTask;
|
||
|
import android.os.Bundle;
|
||
|
import android.support.v7.app.ActionBarActivity;
|
||
|
import android.support.v7.widget.Toolbar;
|
||
9 years ago
|
import android.view.LayoutInflater;
|
||
|
import android.view.Menu;
|
||
9 years ago
|
import android.view.MenuItem;
|
||
|
import android.view.View;
|
||
9 years ago
|
import android.widget.SeekBar;
|
||
|
import android.widget.SeekBar.OnSeekBarChangeListener;
|
||
9 years ago
|
import android.widget.TextView;
|
||
|
|
||
|
public class ReadingActivity extends ActionBarActivity {
|
||
|
|
||
|
private TextView mTitle;
|
||
|
private TextView mBody;
|
||
9 years ago
|
private boolean mInvert;
|
||
|
private String mUrl = null;
|
||
9 years ago
|
private PreferenceManager mPreferences;
|
||
9 years ago
|
private int mTextSize;
|
||
|
private static final float XXLARGE = 30.0f;
|
||
|
private static final float XLARGE = 26.0f;
|
||
|
private static final float LARGE = 22.0f;
|
||
|
private static final float MEDIUM = 18.0f;
|
||
|
private static final float SMALL = 14.0f;
|
||
|
private static final float XSMALL = 10.0f;
|
||
9 years ago
|
|
||
|
@Override
|
||
|
protected void onCreate(Bundle savedInstanceState) {
|
||
9 years ago
|
mPreferences = PreferenceManager.getInstance();
|
||
|
mInvert = mPreferences.getInvertColors();
|
||
9 years ago
|
if (mInvert) {
|
||
|
this.setTheme(R.style.Theme_SettingsTheme_Dark);
|
||
|
}
|
||
9 years ago
|
super.onCreate(savedInstanceState);
|
||
|
setContentView(R.layout.reading_view);
|
||
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||
|
setSupportActionBar(toolbar);
|
||
|
|
||
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||
|
|
||
|
mTitle = (TextView) findViewById(R.id.textViewTitle);
|
||
|
mBody = (TextView) findViewById(R.id.textViewBody);
|
||
|
|
||
9 years ago
|
mTextSize = mPreferences.getReadingTextSize();
|
||
9 years ago
|
mBody.setTextSize(getTextSize(mTextSize));
|
||
9 years ago
|
mTitle.setText(getString(R.string.untitled));
|
||
|
mBody.setText(getString(R.string.loading));
|
||
|
|
||
|
mTitle.setVisibility(View.INVISIBLE);
|
||
|
mBody.setVisibility(View.INVISIBLE);
|
||
|
|
||
|
Intent intent = getIntent();
|
||
|
if (!loadPage(intent)) {
|
||
|
setText(getString(R.string.untitled), getString(R.string.loading_failed));
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
private float getTextSize(int size) {
|
||
|
switch (size) {
|
||
|
case 0:
|
||
|
return XSMALL;
|
||
|
case 1:
|
||
|
return SMALL;
|
||
|
case 2:
|
||
|
return MEDIUM;
|
||
|
case 3:
|
||
|
return LARGE;
|
||
|
case 4:
|
||
|
return XLARGE;
|
||
|
case 5:
|
||
|
return XXLARGE;
|
||
|
default:
|
||
|
return MEDIUM;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||
|
getMenuInflater().inflate(R.menu.reading, menu);
|
||
|
return super.onCreateOptionsMenu(menu);
|
||
|
}
|
||
|
|
||
9 years ago
|
protected boolean loadPage(Intent intent) {
|
||
|
if (intent == null) {
|
||
|
return false;
|
||
|
}
|
||
9 years ago
|
mUrl = intent.getStringExtra(Constants.LOAD_READING_URL);
|
||
|
if (mUrl == null) {
|
||
9 years ago
|
return false;
|
||
|
}
|
||
9 years ago
|
getSupportActionBar().setTitle(Utils.getDomainName(mUrl));
|
||
|
new PageLoader(this).execute(mUrl);
|
||
9 years ago
|
return true;
|
||
|
}
|
||
|
|
||
|
private class PageLoader extends AsyncTask<String, Void, Void> {
|
||
|
|
||
|
private Context mContext;
|
||
|
private ProgressDialog mProgressDialog;
|
||
|
private String mTitleText;
|
||
|
private List<String> mBodyText;
|
||
|
|
||
|
public PageLoader(Context context) {
|
||
|
mContext = context;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void onPreExecute() {
|
||
|
super.onPreExecute();
|
||
|
mProgressDialog = new ProgressDialog(mContext);
|
||
|
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
|
||
|
mProgressDialog.setCancelable(false);
|
||
|
mProgressDialog.setIndeterminate(true);
|
||
|
mProgressDialog.setMessage(mContext.getString(R.string.loading));
|
||
|
mProgressDialog.show();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected Void doInBackground(String... params) {
|
||
|
|
||
|
HtmlFetcher fetcher = new HtmlFetcher();
|
||
|
try {
|
||
|
JResult result = fetcher.fetchAndExtract(params[0], 5000, true);
|
||
|
mTitleText = result.getTitle();
|
||
|
mBodyText = result.getTextList();
|
||
|
} catch (Exception e) {
|
||
|
mTitleText = "";
|
||
|
mBodyText = new ArrayList<>();
|
||
|
e.printStackTrace();
|
||
|
} catch (OutOfMemoryError e) {
|
||
|
System.gc();
|
||
|
mTitleText = "";
|
||
|
mBodyText = new ArrayList<>();
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void onPostExecute(Void result) {
|
||
|
mProgressDialog.dismiss();
|
||
|
if (mTitleText.isEmpty() || mBodyText.isEmpty()) {
|
||
|
setText(getString(R.string.untitled), getString(R.string.loading_failed));
|
||
|
} else {
|
||
|
StringBuilder builder = new StringBuilder();
|
||
|
for (String text : mBodyText) {
|
||
9 years ago
|
builder.append(text).append("\n\n");
|
||
9 years ago
|
}
|
||
|
setText(mTitleText, builder.toString());
|
||
|
}
|
||
|
super.onPostExecute(result);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
private void setText(String title, String body) {
|
||
|
if (mTitle.getVisibility() == View.INVISIBLE) {
|
||
|
mTitle.setAlpha(0.0f);
|
||
|
mTitle.setVisibility(View.VISIBLE);
|
||
|
mTitle.setText(title);
|
||
|
ObjectAnimator animator = ObjectAnimator.ofFloat(mTitle, "alpha", 1.0f);
|
||
|
animator.setDuration(300);
|
||
|
animator.start();
|
||
|
} else {
|
||
|
mTitle.setText(title);
|
||
|
}
|
||
|
|
||
|
if (mBody.getVisibility() == View.INVISIBLE) {
|
||
|
mBody.setAlpha(0.0f);
|
||
|
mBody.setVisibility(View.VISIBLE);
|
||
|
mBody.setText(body);
|
||
|
ObjectAnimator animator = ObjectAnimator.ofFloat(mBody, "alpha", 1.0f);
|
||
|
animator.setDuration(300);
|
||
|
animator.start();
|
||
|
} else {
|
||
|
mBody.setText(body);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||
9 years ago
|
switch (item.getItemId()) {
|
||
|
case R.id.invert_item:
|
||
9 years ago
|
mPreferences.setInvertColors(!mInvert);
|
||
9 years ago
|
Intent read = new Intent(this, ReadingActivity.class);
|
||
|
read.putExtra(Constants.LOAD_READING_URL, mUrl);
|
||
|
startActivity(read);
|
||
|
finish();
|
||
|
break;
|
||
|
case R.id.text_size_item:
|
||
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||
|
LayoutInflater inflater = this.getLayoutInflater();
|
||
|
View view = inflater.inflate(R.layout.seek_layout, null);
|
||
|
final SeekBar bar = (SeekBar) view.findViewById(R.id.text_size_seekbar);
|
||
|
bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
|
||
|
|
||
|
@Override
|
||
|
public void onProgressChanged(SeekBar view, int size, boolean user) {
|
||
|
mBody.setTextSize(getTextSize(size));
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onStartTrackingTouch(SeekBar arg0) {
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onStopTrackingTouch(SeekBar arg0) {
|
||
|
}
|
||
|
|
||
|
});
|
||
|
bar.setMax(5);
|
||
|
bar.setProgress(mTextSize);
|
||
|
builder.setView(view);
|
||
|
builder.setTitle(R.string.size);
|
||
|
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
|
||
|
|
||
|
@Override
|
||
|
public void onClick(DialogInterface arg0, int arg1) {
|
||
|
mTextSize = bar.getProgress();
|
||
|
mBody.setTextSize(getTextSize(mTextSize));
|
||
9 years ago
|
mPreferences.setReadingTextSize(bar.getProgress());
|
||
9 years ago
|
}
|
||
|
|
||
|
});
|
||
|
builder.show();
|
||
|
break;
|
||
|
default:
|
||
|
finish();
|
||
|
break;
|
||
|
}
|
||
9 years ago
|
return super.onOptionsItemSelected(item);
|
||
|
}
|
||
|
}
|