Skip to content

Commit

Permalink
Logging option and 'Report an issue' link
Browse files Browse the repository at this point in the history
Logging option enables/disables log output. Logging is put in both
LogCat and a simple file created in the external storage (hence the
extra permission). #24
  • Loading branch information
ADTC committed Aug 17, 2014
1 parent 1f14cda commit 859b441
Show file tree
Hide file tree
Showing 9 changed files with 507 additions and 267 deletions.
1 change: 1 addition & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
Expand Down
3 changes: 3 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<string name="pref_category_ind_appearance">Indicator appearance</string>
<string name="pref_category_unit_settings">Unit settings</string>
<string name="pref_category_text_appearance">Text appearance</string>
<string name="pref_category_help">Help</string>
<string name="pref_unit_mode">Unit mode</string>
<string name="pref_unit">Unit</string>
<string name="pref_unit_format">Display and format</string>
Expand All @@ -30,6 +31,8 @@
<string name="unit_update_interval">%s ms</string> <!-- see note -->
<string name="pref_font_color">Font color</string>
<string name="pref_font_style">Font style</string>
<string name="pref_enable_logging">Enable logging</string>
<string name="pref_report_issue">Report an issue</string>

<string name="unit_format_hidden">Hidden</string>
<string name="unit_format_fmt_as">Formatted as</string>
Expand Down
2 changes: 2 additions & 0 deletions res/values/values.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,6 @@
<item>I</item>
</string-array>

<string name="url_report_issue">https://github.com/chiehmin/Xposed-NetworkSpeedIndicator/issues</string>

</resources>
17 changes: 17 additions & 0 deletions res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,22 @@
/>
</PreferenceCategory>

<PreferenceCategory
android:title="@string/pref_category_help">

<CheckBoxPreference
android:defaultValue="false"
android:key="enable_logging"
android:title="@string/pref_enable_logging" />

<PreferenceScreen
android:title="@string/pref_report_issue">
<intent
android:action="android.intent.action.VIEW"
android:data="@string/url_report_issue" />
</PreferenceScreen>

</PreferenceCategory>


</PreferenceScreen>
16 changes: 13 additions & 3 deletions src/tw/fatminmin/xposed/networkspeedindicator/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import java.util.HashSet;
import java.util.Set;

import tw.fatminmin.xposed.networkspeedindicator.logger.Log;
import android.content.SharedPreferences;

public final class Common {

private static final String TAG = Common.class.getSimpleName();
public static final String PKG_NAME = "tw.fatminmin.xposed.networkspeedindicator";
public static final String ACTION_SETTINGS_CHANGED = PKG_NAME + ".changed";

Expand All @@ -25,6 +27,7 @@ public final class Common {
public static final String KEY_FONT_COLOR = "font_color";
public static final String KEY_COLOR = "color";
public static final String KEY_FONT_STYLE = "font_style";
public static final String KEY_ENABLE_LOG = "enable_logging";

public static final HashSet<String> DEF_NETWORK_TYPE = new HashSet<String>();
static {
Expand Down Expand Up @@ -57,6 +60,7 @@ public final class Common {
public static final boolean DEF_FONT_COLOR = false;
public static final int DEF_COLOR = android.graphics.Color.LTGRAY;
public static final HashSet<String> DEF_FONT_STYLE = new HashSet<String>();
public static final boolean DEF_ENABLE_LOG = false;

public static final String BIG_UP_TRIANGLE = " \u25B2 ";
public static final String BIG_DOWN_TRIANGLE = " \u25BC ";
Expand All @@ -73,7 +77,7 @@ public static final int getPrefInt(final SharedPreferences pref, final String ke
String value = pref.getString(key, String.valueOf(def_value));
return Integer.parseInt(value);
} catch (Exception e) {
// Do nothing
Log.w(TAG, "Key: ", key, ". Def: ", def_value, ". Exception ignored: ", e);
}
return def_value;
}
Expand All @@ -83,13 +87,12 @@ public static final float getPrefFloat(final SharedPreferences pref, final Strin
String value = pref.getString(key, String.valueOf(def_value));
return Float.parseFloat(value);
} catch (Exception e) {
// Do nothing
Log.w(TAG, "Key: ", key, ". Def: ", def_value, ". Exception ignored: ", e);
}
return def_value;
}

public static final String formatUnit(final int prefUnitMode, final int prefUnitFactor, final Set<String> prefUnitFormat) {

boolean binaryMode = (prefUnitMode == 0 || prefUnitMode == 1);
boolean bitMode = (prefUnitMode == 0 || prefUnitMode == 2);

Expand Down Expand Up @@ -136,5 +139,12 @@ else if (prefUnitFactor == 1)

return unit.toString();
}

public static final void throwException(final Exception e) {
if ((e != null) && (e instanceof RuntimeException))
throw (RuntimeException) e;
else
throw new RuntimeException(e);
}

}
180 changes: 103 additions & 77 deletions src/tw/fatminmin/xposed/networkspeedindicator/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;
import java.util.Map.Entry;

import tw.fatminmin.xposed.networkspeedindicator.logger.Log;
import tw.fatminmin.xposed.networkspeedindicator.widget.GingerBreadPositionCallbackImpl;
import tw.fatminmin.xposed.networkspeedindicator.widget.JellyBeanPositionCallbackImpl;
import tw.fatminmin.xposed.networkspeedindicator.widget.PositionCallbackImpl;
Expand All @@ -32,6 +33,7 @@
public final class Module implements IXposedHookLoadPackage,
IXposedHookInitPackageResources {

private static final String TAG = Module.class.getSimpleName();

private final TextView getClock() {
if(trafficView == null || trafficView.mPositionCallback == null) {
Expand All @@ -45,12 +47,16 @@ private final TextView getClock() {
return null;
}


@Override
public final void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {
if(!lpparam.packageName.equals(PKG_NAME_SYSTEM_UI)) {
return;
}
try {
if(!lpparam.packageName.equals(PKG_NAME_SYSTEM_UI)) {
return;
}
} catch (Exception e) {
Log.e(TAG, "handleLoadPackage failed: ", e);
throw e;
}
try {
// we hook this method to follow alpha changes in kitkat
Class<?> cClock = XposedHelpers.findClass("com.android.systemui.statusbar.policy.Clock", lpparam.classLoader);
Expand All @@ -60,36 +66,44 @@ public final void handleLoadPackage(final LoadPackageParam lpparam) throws Throw
@SuppressLint("NewApi")
@Override
protected final void afterHookedMethod(final MethodHookParam param) throws Throwable {

if(param.thisObject != getClock())
return;

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if(trafficView != null) {
if (statusIcons != null) {
trafficView.setAlpha(statusIcons.getAlpha());
} else if (clock != null) {
trafficView.setAlpha(clock.getAlpha());
}
}
}
try {
if(param.thisObject != getClock())
return;

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if(trafficView != null) {
if (statusIcons != null) {
trafficView.setAlpha(statusIcons.getAlpha());
} else if (clock != null) {
trafficView.setAlpha(clock.getAlpha());
}
}
}
} catch (Exception e) {
Log.e(TAG, "afterHookedMethod (setAlpha) failed: ", e);
throw e;
}
}
});
XposedBridge.hookAllMethods(cClock, "setTextColor", new XC_MethodHook() {
@Override
protected final void afterHookedMethod(final MethodHookParam param) throws Throwable {

if(param.thisObject != getClock())
return;

if(trafficView != null && clock != null) {
trafficView.setTextColor(clock.getCurrentTextColor());
}
try {
if(param.thisObject != getClock())
return;

if(trafficView != null && clock != null) {
trafficView.setTextColor(clock.getCurrentTextColor());
}
} catch (Exception e) {
Log.e(TAG, "afterHookedMethod (setTextColor) failed: ", e);
throw e;
}
}
});
}
catch(Exception e){

Log.e(TAG, "handleLoadPackage failure ignored: ", e);
}
}

Expand All @@ -111,58 +125,70 @@ protected final void afterHookedMethod(final MethodHookParam param) throws Throw
@Override
public final void handleInitPackageResources(final InitPackageResourcesParam resparam)
throws Throwable {
if (!resparam.packageName.equals(PKG_NAME_SYSTEM_UI)) {
return;
}
XResources res = resparam.res;

final Entry<String, String> layoutInfo = findLayoutInfo(res);
if (layoutInfo == null)
return;

res.hookLayout(PKG_NAME_SYSTEM_UI, "layout", layoutInfo.getKey(),
new XC_LayoutInflated() {

@Override
public final void handleLayoutInflated(final LayoutInflatedParam liparam)
throws Throwable {
FrameLayout root = (FrameLayout) liparam.view;

clock = (TextView) root.findViewById(liparam.res.getIdentifier("clock", "id", PKG_NAME_SYSTEM_UI));
statusIcons = (View) root.findViewById(liparam.res.getIdentifier("statusIcons", "id", PKG_NAME_SYSTEM_UI));

if (trafficView == null) {
trafficView = new TrafficView(root.getContext());
trafficView.clock = clock;
}
if (clock != null) {
trafficView.setLayoutParams(clock.getLayoutParams());
trafficView.setTextColor(clock.getCurrentTextColor());
} else {
// gingerbread
trafficView.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT));
trafficView.setTextColor(Color
.parseColor("#33b5e5"));
}
trafficView.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);

if (liparam.res.getIdentifier("status_bar_contents", "id", PKG_NAME_SYSTEM_UI) != 0) {
// kitkat
trafficView.mPositionCallback = new PositionCallbackImpl();
} else if (liparam.res.getIdentifier("notification_icon_area", "id",PKG_NAME_SYSTEM_UI) != 0) {
// jellybean
trafficView.mPositionCallback = new JellyBeanPositionCallbackImpl();
} else if (liparam.res.getIdentifier("notificationIcons", "id", PKG_NAME_SYSTEM_UI) != 0) {
// gingerbread
trafficView.mPositionCallback = new GingerBreadPositionCallbackImpl();
}

trafficView.mPositionCallback.setup(liparam, trafficView);
trafficView.refreshPosition();
}
});
try {
if (!resparam.packageName.equals(PKG_NAME_SYSTEM_UI)) {
return;
}
XResources res = resparam.res;

final Entry<String, String> layoutInfo = findLayoutInfo(res);
if (layoutInfo == null)
return;

res.hookLayout(PKG_NAME_SYSTEM_UI, "layout", layoutInfo.getKey(),
new XC_LayoutInflated() {

@Override
public final void handleLayoutInflated(final LayoutInflatedParam liparam)
throws Throwable {
try {
FrameLayout root = (FrameLayout) liparam.view;

clock = (TextView) root.findViewById(liparam.res.getIdentifier("clock", "id", PKG_NAME_SYSTEM_UI));
statusIcons = (View) root.findViewById(liparam.res.getIdentifier("statusIcons", "id", PKG_NAME_SYSTEM_UI));

if (trafficView == null) {
trafficView = new TrafficView(root.getContext());
trafficView.clock = clock;
}
if (clock != null) {
trafficView.setLayoutParams(clock.getLayoutParams());
trafficView.setTextColor(clock.getCurrentTextColor());
} else {
Log.i(TAG, "Clock: Gingerbread");
trafficView.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT));
trafficView.setTextColor(Color
.parseColor("#33b5e5"));
}
trafficView.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);

if (liparam.res.getIdentifier("status_bar_contents", "id", PKG_NAME_SYSTEM_UI) != 0) {
Log.i(TAG, "PositionCallback: KitKat");
trafficView.mPositionCallback = new PositionCallbackImpl();

} else if (liparam.res.getIdentifier("notification_icon_area", "id",PKG_NAME_SYSTEM_UI) != 0) {
Log.i(TAG, "PositionCallback: Jelly Bean");
trafficView.mPositionCallback = new JellyBeanPositionCallbackImpl();

} else if (liparam.res.getIdentifier("notificationIcons", "id", PKG_NAME_SYSTEM_UI) != 0) {
Log.i(TAG, "PositionCallback: Gingerbread");
trafficView.mPositionCallback = new GingerBreadPositionCallbackImpl();
}

trafficView.mPositionCallback.setup(liparam, trafficView);
trafficView.refreshPosition();
} catch (Exception e) {
Log.e(TAG, "handleLayoutInflated failed: ", e);
throw e;
}
}
});
} catch (Exception e) {
Log.e(TAG, "handleInitPackageResources failed: ", e);
throw e;
}
}

private static final Entry<String, String> findLayoutInfo(final XResources res) {
Expand Down
Loading

0 comments on commit 859b441

Please sign in to comment.