Add StatsActivity

This commit is contained in:
seven332 committed 2015-05-08 16:27:53 +08:00
1 parent b0a21d8487
commit fbced66a4e
9 files changed
+202 -12

No files matched your search

+1
View File
@@ -81,6 +81,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
@@ -20,7 +20,7 @@ import android.content.res.Configuration;
import com.hippo.ehviewer.util.Ui;
public class AbsTranslucentActivity extends AbsActivity {
public class AbsTranslucentActivity extends StatsActivity {
@Override
public void onConfigurationChanged(Configuration newConfig) {
@@ -29,6 +29,11 @@ public class AbsTranslucentActivity extends AbsActivity {
Ui.updateTranslucent(this);
}
@Override
protected boolean isShowStats() {
return true;
}
@Override
protected void onResume() {
super.onResume();
@@ -39,7 +39,6 @@ import com.hippo.ehviewer.AppHandler;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.app.DirSelectDialog;
import com.hippo.ehviewer.app.MaterialAlertDialog;
import com.hippo.ehviewer.content.Messenger;
import com.hippo.ehviewer.data.Data;
import com.hippo.ehviewer.data.GalleryInfo;
import com.hippo.ehviewer.ehclient.ExDownloader;
@@ -49,6 +48,7 @@ import com.hippo.ehviewer.gallery.ui.GLRootView;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.FullScreenHelper;
import com.hippo.ehviewer.util.MathUtils;
import com.hippo.ehviewer.util.Messenger;
import com.hippo.ehviewer.util.Utils;
import com.hippo.ehviewer.widget.ColorView;
import com.hippo.ehviewer.widget.MaterialToast;
@@ -56,7 +56,7 @@ import com.hippo.ehviewer.widget.SlidingLayout;
import java.io.File;
public class GalleryActivity extends AbsActivity
public class GalleryActivity extends StatsActivity
implements GalleryView.GalleryViewListener, SeekBar.OnSeekBarChangeListener,
FullScreenHelper.OnFullScreenBrokenListener, CompoundButton.OnCheckedChangeListener,
SlidingLayout.OnChildHideListener, AdapterView.OnItemSelectedListener,
@@ -266,6 +266,11 @@ public class GalleryActivity extends AbsActivity
mDecodeFormat.setSelection(Config.getDecodeFormatIndex());
}
@Override
protected boolean isShowStats() {
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -455,7 +460,7 @@ public class GalleryActivity extends AbsActivity
}
@Override
public void onReceive(Object obj) {
public void onReceive(int id, Object obj) {
if (obj instanceof Boolean) {
mVolumePage = (Boolean) obj;
}
@@ -50,7 +50,6 @@ import com.hippo.ehviewer.UpdateHelper;
import com.hippo.ehviewer.app.DirSelectDialog;
import com.hippo.ehviewer.app.MaterialAlertDialog;
import com.hippo.ehviewer.app.MaterialProgressDialog;
import com.hippo.ehviewer.content.Messenger;
import com.hippo.ehviewer.data.ApiGalleryInfo;
import com.hippo.ehviewer.data.Data;
import com.hippo.ehviewer.data.DownloadInfo;
@@ -58,14 +57,15 @@ import com.hippo.ehviewer.drawable.MaterialIndicatorDrawable;
import com.hippo.ehviewer.drawable.MaterialIndicatorDrawable.Stroke;
import com.hippo.ehviewer.ehclient.EhClient;
import com.hippo.ehviewer.ehclient.EhInfo;
import com.hippo.ehviewer.miscellaneous.ActivityHelper;
import com.hippo.ehviewer.miscellaneous.FavoriteHelper;
import com.hippo.ehviewer.network.HttpHelper;
import com.hippo.ehviewer.preference.ListPreference;
import com.hippo.ehviewer.miscellaneous.ActivityHelper;
import com.hippo.ehviewer.util.BgThread;
import com.hippo.ehviewer.util.Config;
import com.hippo.ehviewer.util.DialogUtils;
import com.hippo.ehviewer.util.EhUtils;
import com.hippo.ehviewer.miscellaneous.FavoriteHelper;
import com.hippo.ehviewer.util.Messenger;
import com.hippo.ehviewer.util.Secret;
import com.hippo.ehviewer.util.Ui;
import com.hippo.ehviewer.util.Utils;
@@ -0,0 +1,156 @@
/*
* Copyright (C) 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.ui;
import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Debug;
import android.os.Handler;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.hippo.ehviewer.AppHandler;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.util.Constants;
import com.hippo.ehviewer.util.Messenger;
import java.util.Locale;
public abstract class StatsActivity extends AbsActivity {
private static final int DEFAULT_MESSAGE_SIZE = 128;
private static final long STATS_CLOCK_INTERVAL_MS = 1000;
private static final int BYTES_IN_MEGABYTE = 1024 * 1024;
private TextView mStatsTextView;
private boolean mShowStats;
private Runnable mStatsClockTickRunnable;
private Handler mHandler;
private Messenger.Receiver mReceiver = new Messenger.Receiver() {
@Override
public void onReceive(int id, Object obj) {
if (obj instanceof Boolean) {
boolean showStats = (Boolean) obj;
if (mShowStats != showStats) {
mShowStats = showStats;
ViewGroup vg = (ViewGroup) getWindow().getDecorView();
if (showStats) {
vg.addView(mStatsTextView, generateLayoutParams());
updateStats();
scheduleNextStatsClockTick();
} else {
vg.removeView(mStatsTextView);
cancelNextStatsClockTick();
}
}
}
}
};
@SuppressLint("RtlHardcoded")
private ViewGroup.LayoutParams generateLayoutParams() {
Resources resources = getResources();
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.TOP | Gravity.RIGHT;
lp.topMargin = resources.getDimensionPixelOffset(R.dimen.stats_text_view_padding_top);
lp.rightMargin = resources.getDimensionPixelOffset(R.dimen.stats_text_view_padding_right);
return lp;
}
protected abstract boolean isShowStats();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mStatsTextView = new TextView(this);
mHandler = AppHandler.getInstance();
mStatsClockTickRunnable = new Runnable() {
@Override
public void run() {
updateStats();
scheduleNextStatsClockTick();
}
};
Messenger.getInstance().register(
Constants.MESSENGER_ID_SHOW_APP_STATUS, mReceiver);
if (isShowStats()) {
mShowStats = true;
((ViewGroup) getWindow().getDecorView()).addView(mStatsTextView,
generateLayoutParams());
} else {
mShowStats = false;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Messenger.getInstance().unregister(Constants.MESSENGER_ID_SHOW_APP_STATUS, mReceiver);
}
@Override
protected void onResume() {
super.onResume();
if (mShowStats) {
updateStats();
scheduleNextStatsClockTick();
}
}
@Override
protected void onPause() {
super.onPause();
if (mShowStats) {
cancelNextStatsClockTick();
}
}
private void scheduleNextStatsClockTick() {
mHandler.postDelayed(mStatsClockTickRunnable, STATS_CLOCK_INTERVAL_MS);
}
private void cancelNextStatsClockTick() {
mHandler.removeCallbacks(mStatsClockTickRunnable);
}
private void updateStats() {
final Runtime runtime = Runtime.getRuntime();
final long heapMemory = runtime.totalMemory() - runtime.freeMemory();
final StringBuilder sb = new StringBuilder(DEFAULT_MESSAGE_SIZE);
appendSize(sb, "Java heap size: ", heapMemory, "\n");
appendSize(sb, "Native heap size: ", Debug.getNativeHeapAllocatedSize(), "\n");
mStatsTextView.setText(sb.toString());
}
private static void appendSize(StringBuilder sb, String prefix, long bytes, String suffix) {
String value = String.format(Locale.getDefault(), "%.2f", (float) bytes / BYTES_IN_MEGABYTE);
appendValue(sb, prefix, value + " MB", suffix);
}
private static void appendValue(StringBuilder sb, String prefix, String value, String suffix) {
sb.append(prefix).append(value).append(suffix);
}
}
@@ -22,7 +22,6 @@ import android.content.pm.ActivityInfo;
import android.os.Environment;
import android.preference.PreferenceManager;
import com.hippo.ehviewer.content.Messenger;
import com.hippo.ehviewer.data.ListUrls;
import com.hippo.ehviewer.ehclient.EhClient;
@@ -28,4 +28,11 @@ public final class Constants {
public static final int MAX_REDIRECTS = 5;
public static final int BUFFER_SIZE = 4096;
public static final int MESSENGER_ID_SHOW_APP_STATUS;
static {
Messenger messenger = Messenger.getInstance();
MESSENGER_ID_SHOW_APP_STATUS = messenger.newId();
}
}
@@ -1,9 +1,23 @@
package com.hippo.ehviewer.content;
/*
* Copyright (C) 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.util;
import android.util.SparseArray;
import com.hippo.ehviewer.AppHandler;
import com.hippo.ehviewer.util.IntIdGenerator;
import java.util.HashSet;
import java.util.Set;
@@ -40,7 +54,7 @@ public class Messenger {
Set<Receiver> receiverSet = mReceiverSetMap.get(id);
if (receiverSet != null) {
for (Receiver receiver : receiverSet) {
receiver.onReceive(obj);
receiver.onReceive(id, obj);
}
}
}
@@ -68,6 +82,6 @@ public class Messenger {
}
public interface Receiver {
void onReceive (final Object obj);
void onReceive(final int id, final Object obj);
}
}
+3
View File
@@ -41,4 +41,7 @@
<dimen name="lock_pattern_dot_size">12dp</dimen>
<dimen name="lock_pattern_dot_size_activated">28dp</dimen>
<dimen name="stats_text_view_padding_top">24dp</dimen>
<dimen name="stats_text_view_padding_right">16dp</dimen>
</resources>