some refactoring, no functionality change

This commit is contained in:
tibbi 2016-06-19 10:59:24 +02:00
parent 1e20777ffb
commit f6ae35dd09
7 changed files with 100 additions and 84 deletions

View File

@ -10,7 +10,7 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:name=".activities.MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
@ -20,7 +20,7 @@
</activity>
<activity
android:name=".MyWidgetConfigure"
android:name=".activities.WidgetConfigureActivity"
android:screenOrientation="portrait"
android:theme="@style/MyWidgetConfigTheme">
<intent-filter>
@ -29,12 +29,12 @@
</activity>
<activity
android:name=".AboutActivity"
android:name=".activities.AboutActivity"
android:label="@string/about"
android:screenOrientation="portrait"/>
<activity
android:name=".LicenseActivity"
android:name=".activities.LicenseActivity"
android:label="@string/third_party_licences"
android:screenOrientation="portrait"/>

View File

@ -9,39 +9,41 @@ import android.content.SharedPreferences;
import android.graphics.Color;
import android.widget.RemoteViews;
import com.simplemobiletools.notes.activities.MainActivity;
public class MyWidgetProvider extends AppWidgetProvider {
private static SharedPreferences prefs;
private RemoteViews remoteViews;
private static SharedPreferences mPrefs;
private static RemoteViews mRemoteViews;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
initVariables(context);
final int defaultColor = context.getResources().getColor(R.color.dark_grey);
final int newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor);
final int newTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
remoteViews.setInt(R.id.notes_view, "setBackgroundColor", newBgColor);
remoteViews.setInt(R.id.notes_view, "setTextColor", newTextColor);
final int newBgColor = mPrefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor);
final int newTextColor = mPrefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
mRemoteViews.setInt(R.id.notes_view, "setBackgroundColor", newBgColor);
mRemoteViews.setInt(R.id.notes_view, "setTextColor", newTextColor);
for (int widgetId : appWidgetIds) {
updateWidget(appWidgetManager, widgetId, remoteViews);
updateWidget(appWidgetManager, widgetId, mRemoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private void initVariables(Context context) {
prefs = context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
mPrefs = context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
setupAppOpenIntent(R.id.notes_holder, context);
}
private void setupAppOpenIntent(int id, Context context) {
final Intent intent = new Intent(context, MainActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(id, pendingIntent);
mRemoteViews.setOnClickPendingIntent(id, pendingIntent);
}
private void updateWidget(AppWidgetManager widgetManager, int widgetId, RemoteViews remoteViews) {
final String text = prefs.getString(Constants.TEXT, "");
final String text = mPrefs.getString(Constants.TEXT, "");
remoteViews.setTextViewText(R.id.notes_view, text);
widgetManager.updateAppWidget(widgetId, remoteViews);
}

View File

@ -1,4 +1,4 @@
package com.simplemobiletools.notes;
package com.simplemobiletools.notes.activities;
import android.content.Intent;
import android.content.res.Resources;
@ -8,6 +8,9 @@ import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
import com.simplemobiletools.notes.BuildConfig;
import com.simplemobiletools.notes.R;
import java.util.Calendar;
import butterknife.BindView;
@ -15,17 +18,18 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
public class AboutActivity extends AppCompatActivity {
@BindView(R.id.about_copyright) TextView copyright;
@BindView(R.id.about_version) TextView version;
@BindView(R.id.about_email) TextView emailTV;
private Resources res;
@BindView(R.id.about_copyright) TextView mCopyright;
@BindView(R.id.about_version) TextView mVersion;
@BindView(R.id.about_email) TextView mEmailTV;
private static Resources mRes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
ButterKnife.bind(this);
res = getResources();
mRes = getResources();
setupEmail();
setupVersion();
@ -33,23 +37,23 @@ public class AboutActivity extends AppCompatActivity {
}
private void setupEmail() {
final String email = res.getString(R.string.email);
final String appName = res.getString(R.string.app_name);
final String email = mRes.getString(R.string.email);
final String appName = mRes.getString(R.string.app_name);
final String href = "<a href=\"mailto:" + email + "?subject=" + appName + "\">" + email + "</a>";
emailTV.setText(Html.fromHtml(href));
emailTV.setMovementMethod(LinkMovementMethod.getInstance());
mEmailTV.setText(Html.fromHtml(href));
mEmailTV.setMovementMethod(LinkMovementMethod.getInstance());
}
private void setupVersion() {
final String versionName = BuildConfig.VERSION_NAME;
final String versionText = String.format(res.getString(R.string.version), versionName);
version.setText(versionText);
final String versionText = String.format(mRes.getString(R.string.version), versionName);
mVersion.setText(versionText);
}
private void setupCopyright() {
final int year = Calendar.getInstance().get(Calendar.YEAR);
final String copyrightText = String.format(res.getString(R.string.copyright), year);
copyright.setText(copyrightText);
final String copyrightText = String.format(mRes.getString(R.string.copyright), year);
mCopyright.setText(copyrightText);
}
@OnClick(R.id.about_license)

View File

@ -1,14 +1,17 @@
package com.simplemobiletools.notes;
package com.simplemobiletools.notes.activities;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.simplemobiletools.notes.R;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class LicenseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

View File

@ -1,4 +1,4 @@
package com.simplemobiletools.notes;
package com.simplemobiletools.notes.activities;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
@ -8,18 +8,22 @@ import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import com.simplemobiletools.notes.Constants;
import com.simplemobiletools.notes.MyWidgetProvider;
import com.simplemobiletools.notes.R;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
private SharedPreferences prefs;
@BindView(R.id.notes_view) EditText notesView;
@BindView(R.id.notes_view) EditText mNotesView;
private SharedPreferences mPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -27,15 +31,14 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
final String text = prefs.getString(Constants.TEXT, "");
notesView.setText(text);
mPrefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
final String text = mPrefs.getString(Constants.TEXT, "");
mNotesView.setText(text);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@ -55,8 +58,8 @@ public class MainActivity extends AppCompatActivity {
}
private void saveText() {
final String text = notesView.getText().toString().trim();
prefs.edit().putString(Constants.TEXT, text).apply();
final String text = mNotesView.getText().toString().trim();
mPrefs.edit().putString(Constants.TEXT, text).apply();
Toast.makeText(this, getResources().getString(R.string.text_saved), Toast.LENGTH_SHORT).show();
hideKeyboard();
@ -65,7 +68,7 @@ public class MainActivity extends AppCompatActivity {
private void hideKeyboard() {
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(notesView.getWindowToken(), 0);
imm.hideSoftInputFromWindow(mNotesView.getWindowToken(), 0);
}
private void updateWidget() {

View File

@ -1,4 +1,4 @@
package com.simplemobiletools.notes;
package com.simplemobiletools.notes.activities;
import android.appwidget.AppWidgetManager;
import android.content.Context;
@ -13,23 +13,27 @@ import android.widget.RemoteViews;
import android.widget.SeekBar;
import android.widget.TextView;
import com.simplemobiletools.notes.Constants;
import com.simplemobiletools.notes.MyWidgetProvider;
import com.simplemobiletools.notes.R;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yuku.ambilwarna.AmbilWarnaDialog;
public class MyWidgetConfigure extends AppCompatActivity {
@BindView(R.id.config_bg_color) View bgColorPicker;
@BindView(R.id.config_bg_seekbar) SeekBar bgSeekBar;
@BindView(R.id.config_text_color) View textColorPicker;
@BindView(R.id.notes_view) TextView notesView;
@BindView(R.id.config_save) Button saveBtn;
public class WidgetConfigureActivity extends AppCompatActivity {
@BindView(R.id.config_bg_color) View mBgColorPicker;
@BindView(R.id.config_bg_seekbar) SeekBar mBgSeekBar;
@BindView(R.id.config_text_color) View mTextColorPicker;
@BindView(R.id.notes_view) TextView mNotesView;
@BindView(R.id.config_save) Button mSaveBtn;
private int widgetId;
private int bgColor;
private int bgColorWithoutTransparency;
private float bgAlpha;
private int textColor;
private float mBgAlpha;
private int mWidgetId;
private int mBgColor;
private int mBgColorWithoutTransparency;
private int mTextColor;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -42,28 +46,28 @@ public class MyWidgetConfigure extends AppCompatActivity {
final Intent intent = getIntent();
final Bundle extras = intent.getExtras();
if (extras != null)
widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
mWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
finish();
}
private void initVariables() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, 1);
if (bgColor == 1) {
bgColor = Color.BLACK;
bgAlpha = .2f;
mBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, 1);
if (mBgColor == 1) {
mBgColor = Color.BLACK;
mBgAlpha = .2f;
} else {
bgAlpha = Color.alpha(bgColor) / (float) 255;
mBgAlpha = Color.alpha(mBgColor) / (float) 255;
}
bgColorWithoutTransparency = Color.rgb(Color.red(bgColor), Color.green(bgColor), Color.blue(bgColor));
bgSeekBar.setOnSeekBarChangeListener(bgSeekbarChangeListener);
bgSeekBar.setProgress((int) (bgAlpha * 100));
mBgColorWithoutTransparency = Color.rgb(Color.red(mBgColor), Color.green(mBgColor), Color.blue(mBgColor));
mBgSeekBar.setOnSeekBarChangeListener(bgSeekbarChangeListener);
mBgSeekBar.setProgress((int) (mBgAlpha * 100));
updateBackgroundColor();
textColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, getResources().getColor(R.color.colorPrimary));
mTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, getResources().getColor(R.color.colorPrimary));
updateTextColor();
}
@ -71,53 +75,53 @@ public class MyWidgetConfigure extends AppCompatActivity {
public void saveConfig() {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
final RemoteViews views = new RemoteViews(getPackageName(), R.layout.activity_main);
views.setInt(R.id.notes_view, "setBackgroundColor", bgColor);
appWidgetManager.updateAppWidget(widgetId, views);
views.setInt(R.id.notes_view, "setBackgroundColor", mBgColor);
appWidgetManager.updateAppWidget(mWidgetId, views);
storeWidgetBackground();
requestWidgetUpdate();
final Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
private void storeWidgetBackground() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
prefs.edit().putInt(Constants.WIDGET_BG_COLOR, bgColor).apply();
prefs.edit().putInt(Constants.WIDGET_TEXT_COLOR, textColor).apply();
prefs.edit().putInt(Constants.WIDGET_BG_COLOR, mBgColor).apply();
prefs.edit().putInt(Constants.WIDGET_TEXT_COLOR, mTextColor).apply();
}
private void requestWidgetUpdate() {
final Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{widgetId});
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{mWidgetId});
sendBroadcast(intent);
}
private void updateBackgroundColor() {
bgColor = adjustAlpha(bgColorWithoutTransparency, bgAlpha);
notesView.setBackgroundColor(bgColor);
bgColorPicker.setBackgroundColor(bgColor);
saveBtn.setBackgroundColor(bgColor);
mBgColor = adjustAlpha(mBgColorWithoutTransparency, mBgAlpha);
mNotesView.setBackgroundColor(mBgColor);
mBgColorPicker.setBackgroundColor(mBgColor);
mSaveBtn.setBackgroundColor(mBgColor);
}
private void updateTextColor() {
textColorPicker.setBackgroundColor(textColor);
saveBtn.setTextColor(textColor);
notesView.setTextColor(textColor);
mTextColorPicker.setBackgroundColor(mTextColor);
mSaveBtn.setTextColor(mTextColor);
mNotesView.setTextColor(mTextColor);
}
@OnClick(R.id.config_bg_color)
public void pickBackgroundColor() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, bgColorWithoutTransparency, new AmbilWarnaDialog.OnAmbilWarnaListener() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, mBgColorWithoutTransparency, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
bgColorWithoutTransparency = color;
mBgColorWithoutTransparency = color;
updateBackgroundColor();
}
});
@ -127,14 +131,14 @@ public class MyWidgetConfigure extends AppCompatActivity {
@OnClick(R.id.config_text_color)
public void pickTextColor() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, textColor, new AmbilWarnaDialog.OnAmbilWarnaListener() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, mTextColor, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
textColor = color;
mTextColor = color;
updateTextColor();
}
});
@ -145,7 +149,7 @@ public class MyWidgetConfigure extends AppCompatActivity {
private SeekBar.OnSeekBarChangeListener bgSeekbarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
bgAlpha = (float) progress / (float) 100;
mBgAlpha = (float) progress / (float) 100;
updateBackgroundColor();
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="com.simplemobiletools.notes.MyWidgetConfigure"
android:configure="com.simplemobiletools.notes.activities.WidgetConfigureActivity"
android:initialLayout="@layout/widget"
android:minHeight="30dp"
android:minWidth="30dp"