adding raw stream debug

This commit is contained in:
Mariotaku Lee 2017-03-09 12:42:07 +08:00
parent 89b70936bd
commit 4d4f6f382d
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
7 changed files with 133 additions and 15 deletions

View File

@ -36,7 +36,7 @@ subprojects {
Kotlin : '1.1.0', Kotlin : '1.1.0',
SupportLib : '25.2.0', SupportLib : '25.2.0',
MariotakuCommons: '0.9.11', MariotakuCommons: '0.9.11',
RestFu : '0.9.35', RestFu : '0.9.40',
ObjectCursor : '0.9.16', ObjectCursor : '0.9.16',
PlayServices : '10.2.0', PlayServices : '10.2.0',
MapsUtils : '0.4.4', MapsUtils : '0.4.4',

View File

@ -31,6 +31,7 @@ import com.squareup.leakcanary.RefWatcher;
import org.mariotaku.twidere.BuildConfig; import org.mariotaku.twidere.BuildConfig;
import org.mariotaku.twidere.util.net.NoIntercept; import org.mariotaku.twidere.util.net.NoIntercept;
import org.mariotaku.twidere.util.stetho.AccountsDumper; import org.mariotaku.twidere.util.stetho.AccountsDumper;
import org.mariotaku.twidere.util.stetho.RawStreamDumper;
import java.io.IOException; import java.io.IOException;
@ -69,6 +70,7 @@ public class DebugModeUtils {
public Iterable<DumperPlugin> get() { public Iterable<DumperPlugin> get() {
return new Stetho.DefaultDumperPluginsBuilder(application) return new Stetho.DefaultDumperPluginsBuilder(application)
.provide(new AccountsDumper(application)) .provide(new AccountsDumper(application))
.provide(new RawStreamDumper(application))
.finish(); .finish();
} }
}) })

View File

@ -28,7 +28,6 @@ import com.bluelinelabs.logansquare.LoganSquare
import com.facebook.stetho.dumpapp.DumperContext import com.facebook.stetho.dumpapp.DumperContext
import com.facebook.stetho.dumpapp.DumperPlugin import com.facebook.stetho.dumpapp.DumperPlugin
import org.apache.commons.cli.GnuParser import org.apache.commons.cli.GnuParser
import org.apache.commons.cli.Option
import org.apache.commons.cli.Options import org.apache.commons.cli.Options
import org.mariotaku.ktextension.HexColorFormat import org.mariotaku.ktextension.HexColorFormat
import org.mariotaku.ktextension.subArray import org.mariotaku.ktextension.subArray
@ -70,8 +69,8 @@ class AccountsDumper(val context: Context) : DumperPlugin {
"import" -> { "import" -> {
val subCommandArgs = argsAsList.subArray(1..argsAsList.lastIndex) val subCommandArgs = argsAsList.subArray(1..argsAsList.lastIndex)
val options = Options().apply { val options = Options().apply {
addRequiredOption(Option("p", "password", true, "Account encryption password")) addRequiredOption("p", "password", true, "Account encryption password")
addRequiredOption(Option("i", "input", true, "Accounts data file")) addRequiredOption("i", "input", true, "Accounts data file")
} }
val commandLine = parser.parse(options, subCommandArgs) val commandLine = parser.parse(options, subCommandArgs)
try { try {
@ -86,8 +85,8 @@ class AccountsDumper(val context: Context) : DumperPlugin {
"export" -> { "export" -> {
val subCommandArgs = argsAsList.subArray(1..argsAsList.lastIndex) val subCommandArgs = argsAsList.subArray(1..argsAsList.lastIndex)
val options = Options().apply { val options = Options().apply {
addRequiredOption(Option("p", "password", true, "Account encryption password")) addRequiredOption("p", "password", true, "Account encryption password")
addRequiredOption(Option("o", "output", true, "Accounts data file")) addRequiredOption("o", "output", true, "Accounts data file")
} }
val commandLine = parser.parse(options, subCommandArgs) val commandLine = parser.parse(options, subCommandArgs)
try { try {
@ -99,6 +98,13 @@ class AccountsDumper(val context: Context) : DumperPlugin {
e.printStackTrace(dumpContext.stderr) e.printStackTrace(dumpContext.stderr)
} }
} }
"list" -> {
val am = AccountManager.get(context)
val accounts = AccountUtils.getAllAccountDetails(am, true)
accounts.forEach {
dumpContext.stdout.println(it.key)
}
}
else -> { else -> {
dumpContext.stderr.println("Usage: accounts [import|export] -p <password>") dumpContext.stderr.println("Usage: accounts [import|export] -p <password>")
} }
@ -172,11 +178,6 @@ class AccountsDumper(val context: Context) : DumperPlugin {
return bb.array() return bb.array()
} }
private fun Options.addRequiredOption(option: Option) {
option.isRequired = true
addOption(option)
}
private fun generateSecret(password: String): SecretKeySpec { private fun generateSecret(password: String): SecretKeySpec {
val spec = PBEKeySpec(password.toCharArray(), salt, 65536, 256) val spec = PBEKeySpec(password.toCharArray(), salt, 65536, 256)
return SecretKeySpec(factory.generateSecret(spec).encoded, "AES") return SecretKeySpec(factory.generateSecret(spec).encoded, "AES")

View File

@ -0,0 +1,34 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util.stetho
import org.apache.commons.cli.Option
import org.apache.commons.cli.Options
/**
* Created by mariotaku on 2017/3/9.
*/
internal fun Options.addRequiredOption(opt: String, longOpt: String? = null, hasArg: Boolean = false,
description: String) {
val option = Option(opt, longOpt, hasArg, description)
option.isRequired = true
addOption(option)
}

View File

@ -0,0 +1,76 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util.stetho
import android.accounts.AccountManager
import android.content.Context
import com.facebook.stetho.dumpapp.DumpException
import com.facebook.stetho.dumpapp.DumperContext
import com.facebook.stetho.dumpapp.DumperPlugin
import org.apache.commons.cli.GnuParser
import org.apache.commons.cli.Options
import org.apache.commons.cli.ParseException
import org.mariotaku.microblog.library.MicroBlogException
import org.mariotaku.microblog.library.twitter.TwitterUserStream
import org.mariotaku.restfu.callback.RawCallback
import org.mariotaku.restfu.http.HttpResponse
import org.mariotaku.twidere.extension.model.getCredentials
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.model.util.AccountUtils
/**
* Created by mariotaku on 2017/3/9.
*/
class RawStreamDumper(val context: Context) : DumperPlugin {
override fun dump(dumpContext: DumperContext) {
val parser = GnuParser()
val options = Options()
options.addRequiredOption("a", "account", true, "Account key")
val cmdLine = try {
parser.parse(options, dumpContext.argsAsList.toTypedArray())
} catch (e: ParseException) {
throw DumpException(e.message)
}
val accountKey = UserKey.valueOf(cmdLine.getOptionValue("account"))
val am = AccountManager.get(context)
val account = AccountUtils.findByAccountKey(am, accountKey) ?: return
val credentials = account.getCredentials(am)
val userStream = credentials.newMicroBlogInstance(context, account.type,
cls = TwitterUserStream::class.java)
userStream.getUserStreamRaw(object : RawCallback<MicroBlogException> {
override fun result(result: HttpResponse) {
dumpContext.stdout.println("Response: ${result.status}")
dumpContext.stdout.println("Headers: ${result.headers}")
result.body.writeTo(dumpContext.stdout)
}
override fun error(exception: MicroBlogException) {
exception.printStackTrace(dumpContext.stderr)
}
})
}
override fun getName() = "raw_stream"
}

View File

@ -19,7 +19,9 @@
package org.mariotaku.microblog.library.twitter; package org.mariotaku.microblog.library.twitter;
import org.mariotaku.microblog.library.MicroBlogException;
import org.mariotaku.restfu.annotation.method.GET; import org.mariotaku.restfu.annotation.method.GET;
import org.mariotaku.restfu.callback.RawCallback;
/** /**
* Created by mariotaku on 15/5/26. * Created by mariotaku on 15/5/26.
@ -29,4 +31,7 @@ public interface TwitterUserStream {
@GET("/user.json") @GET("/user.json")
void getUserStream(UserStreamCallback callback); void getUserStream(UserStreamCallback callback);
@GET("/user.json")
void getUserStreamRaw(RawCallback<MicroBlogException> callback);
} }

View File

@ -46,14 +46,14 @@ import java.io.InputStreamReader;
/** /**
* Created by mariotaku on 15/5/26. * Created by mariotaku on 15/5/26.
*/ */
public abstract class UserStreamCallback implements RawCallback { public abstract class UserStreamCallback implements RawCallback<MicroBlogException> {
private boolean connected; private boolean connected;
private boolean disconnected; private boolean disconnected;
@Override @Override
public final void result(final HttpResponse response) throws IOException { public final void result(final HttpResponse response) throws MicroBlogException, IOException {
if (!response.isSuccessful()) { if (!response.isSuccessful()) {
final MicroBlogException cause = new MicroBlogException(); final MicroBlogException cause = new MicroBlogException();
cause.setHttpResponse(response); cause.setHttpResponse(response);
@ -153,7 +153,7 @@ public abstract class UserStreamCallback implements RawCallback {
@Override @Override
public final void error(final Exception cause) { public final void error(final MicroBlogException cause) {
onException(cause); onException(cause);
} }