Skip to content

Commit

Permalink
add settings ui
Browse files Browse the repository at this point in the history
  • Loading branch information
matsumo committed Feb 19, 2014
1 parent a075ef0 commit d2e5e03
Show file tree
Hide file tree
Showing 8 changed files with 436 additions and 22 deletions.
11 changes: 11 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
<meta-data android:value="true" android:name="xposedmodule"/>
<meta-data android:value="2.1*" android:name="xposedminversion"/>
<meta-data android:value="@string/app_desription" android:name="xposeddescription"/>
<activity android:name=".Settings" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AppListActivity"
android:label="@string/applist_title"
android:exported="false" >
</activity>
</application>

</manifest>
Expand Down
40 changes: 40 additions & 0 deletions res/layout/list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>

<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/msg_save" />

</LinearLayout>
<!--
~ t4jckcs patcher Copyright (C) 2014 matsumo All rights reserved.
~
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you 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 distributed under the License is distributed on an
~ "AS IS" BASIS, 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.
-->
33 changes: 33 additions & 0 deletions res/layout/list_row1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />

</LinearLayout>
<!--
~ t4jckcs patcher Copyright (C) 2014 matsumo All rights reserved.
~
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you 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 distributed under the License is distributed on an
~ "AS IS" BASIS, 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.
-->
20 changes: 18 additions & 2 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@
<resources>

<string name="app_name">t4jckcs patcher</string>
<string name="app_desription">consumer key / consumer secret patcher for twitter4j used application</string>

<string name="app_desription">consumer key / consumer secret patcher for twitter4j used applications</string>
<string name="applist_title">select target applications</string>

<string name="msg_saved">saved!</string>
<string name="msg_save">SAVE</string>
<string name="msg_ck_title">consumer key</string>
<string name="msg_ck_dialog_title">enter consumer key</string>
<string name="msg_cs_title">consumer secret</string>
<string name="msg_cs_dialog_title">enter consumer secret</string>
<string name="msg_target_apps">target applications</string>

<string name="donation_title">make a donation</string>
<string name="donation_summary">please support developer, if you like this app.</string>

<!-- test ck/cs use only oob -->
<string name="default_value_ck">yN3DUNVO0Me63IAQdhTfCA</string>
<string name="default_value_cs">c768oTKdzAjIYCmpSNIdZbGaG0t6rOhSFQP0S5uC79g</string>

</resources>
<!--
~ t4jckcs patcher Copyright (C) 2014 matsumo All rights reserved.
Expand Down
48 changes: 48 additions & 0 deletions res/xml/settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="defaultCK"
android:title="@string/msg_ck_title"
android:dialogTitle="@string/msg_ck_dialog_title"
android:defaultValue="@string/default_value_ck"
/>
<EditTextPreference
android:key="defaultCS"
android:title="@string/msg_cs_title"
android:dialogTitle="@string/msg_cs_dialog_title"
android:defaultValue="@string/default_value_cs"
/>
<PreferenceScreen
android:key="targetApps"
android:title="@string/msg_target_apps"
/>

<PreferenceCategory android:title=" " >
<PreferenceScreen
android:key="donation"
android:title="@string/donation_title"
android:summary="@string/donation_summary"
/>
</PreferenceCategory>
</PreferenceScreen>
<!--
~ t4jckcs patcher Copyright (C) 2014 matsumo All rights reserved.
~
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you 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 distributed under the License is distributed on an
~ "AS IS" BASIS, 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.
-->
205 changes: 205 additions & 0 deletions src/com/matsumo/t4jckcsp/AppListActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/**
* t4jckcs patcher Copyright (C) 2014 matsumo All rights reserved.
*
* 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 distributed under the License is distributed on an "AS IS"
* BASIS, 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.matsumo.t4jckcsp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import dalvik.system.PathClassLoader;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;

public class AppListActivity extends ListActivity {
public static final String EXTRA_PREFS_KEY_NAME = "EXTRA_PREFS_KEY_NAME";

private SharedPreferences pref;
private String pref_key;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
if (intent != null) {
pref_key = intent.getStringExtra(EXTRA_PREFS_KEY_NAME);
}
if (pref_key == null) {
Toast.makeText(this, "bad param!", Toast.LENGTH_SHORT).show();
finish();
return;
}
pref = PreferenceManager.getDefaultSharedPreferences(this);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.list);

((Button) findViewById(R.id.button1))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
saveItems();
finish();
}
});

new GetAppListTask().execute();
}

class PackageInfo {
public String pkg, name;
public boolean checked;
}

class BooleanListAdapter extends ArrayAdapter<PackageInfo> {
protected LayoutInflater inflater;
private List<PackageInfo> items;

public BooleanListAdapter(Context context, int rowLayoutResourceId,
List<PackageInfo> items) {
super(context, rowLayoutResourceId, items);
this.items = items;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.list_row1, null);
}
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
final int p = position;
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
items.get(p).checked = isChecked;
}
});
checkBox.setChecked(items.get(position).checked);
checkBox.setText(items.get(position).name);
return view;
}
}

class GetAppListTask extends AsyncTask<Integer, Integer, Boolean> {
ArrayList<PackageInfo> appInfo = new ArrayList<PackageInfo>();
protected boolean isStop = false;

@Override
protected void onPreExecute() {
setProgressBarIndeterminateVisibility(true);
}

@Override
protected Boolean doInBackground(Integer... params) {
PackageManager pm = AppListActivity.this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
if (isStop) return false;
Collections.sort(list, new ResolveInfo.DisplayNameComparator(pm));

Set<String> items = loadItems(pref, pref_key, new HashSet<String>());
appInfo.clear();
for (ResolveInfo info : list) {
if (isStop) return false;
if (!isUseTwitter4j(info.activityInfo.packageName)) continue;
PackageInfo p = new PackageInfo();
p.pkg = info.activityInfo.packageName;
p.name = info.loadLabel(pm).toString();
p.checked = items.contains(p.pkg);
appInfo.add(p);
}
return true;
}

@Override
protected void onPostExecute(Boolean result) {
setProgressBarIndeterminateVisibility(false);
if (result) {
setListAdapter(new BooleanListAdapter(AppListActivity.this,
R.layout.list_row1, appInfo));
}
}

@Override
protected void onCancelled() {
isStop = true;
}
}

private void saveItems() {
Set<String> items = new HashSet<String>();
if (getListAdapter() == null || getListAdapter().getCount() == 0) return;
for (int i = 0; i < getListAdapter().getCount(); i++) {
PackageInfo info = (PackageInfo) getListAdapter().getItem(i);
if (info.checked) items.add(info.pkg);
}
saveItems(pref, pref_key, items);
Toast.makeText(this, R.string.msg_saved, Toast.LENGTH_SHORT).show();
}

private static Set<String> loadItems(SharedPreferences prefs,
String prefKey, Set<String> defaultItems) {
if (prefs == null)
return defaultItems;
Set<String> items = prefs.getStringSet(prefKey, defaultItems);
return items;
}

private static void saveItems(SharedPreferences prefs, String prefKey, Set<String> items) {
if (prefs == null || items == null) return;
SharedPreferences.Editor editor = prefs.edit();
editor.putStringSet(prefKey, items);
editor.commit();
}

private boolean isUseTwitter4j(String pkg) {
boolean r = false;
// http://noxi515.blogspot.jp/2011/12/androidapkclassandroid237.html
try {
ApplicationInfo info = getPackageManager().getApplicationInfo(pkg, 0);
ClassLoader loader = new PathClassLoader(info.sourceDir + ":",
getClassLoader().getParent());
Class.forName("twitter4j.conf.ConfigurationBase", true, loader);
r = true;
} catch (Exception e) {
}
return r;
}
}
Loading

0 comments on commit d2e5e03

Please sign in to comment.