Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
morfeusys committed Feb 2, 2016
0 parents commit 47c915f
Show file tree
Hide file tree
Showing 51 changed files with 2,374 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
28 changes: 28 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
applicationId "com.broadlink.control"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.google.code.gson:gson:2.3'
}
Binary file added app/libs/BLNetwork.jar
Binary file not shown.
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/morfeusys/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.broadlink.control;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
42 changes: 42 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadlink.control" >

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ButtonsActivity"/>
<receiver android:name=".BroadlinkReceiver">
<intent-filter>
<action android:name="com.broadlink.control.action.QUERY"/>
<action android:name="com.broadlink.control.action.BUTTON"/>
</intent-filter>
</receiver>
</application>

</manifest>
22 changes: 22 additions & 0 deletions app/src/main/java/com/broadlink/control/BroadlinkReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.broadlink.control;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
* Created by morfeusys on 01.02.16.
*/
public class BroadlinkReceiver extends BroadcastReceiver {
public static final String EXTRA_TEXT = "text";

@Override
public void onReceive(Context context, Intent intent) {
String text = intent.getStringExtra(EXTRA_TEXT);
if ("com.broadlink.control.action.QUERY".equals(intent.getAction())) {
BroadlinkUtil.processFunction(context, text);
} else {
BroadlinkUtil.processButton(context, text);
}
}
}
89 changes: 89 additions & 0 deletions app/src/main/java/com/broadlink/control/BroadlinkUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.broadlink.control;

import android.content.Context;

import com.broadlink.control.api.BroadlinkAPI;
import com.broadlink.control.api.DeviceInfo;
import com.broadlink.control.model.Code;
import com.broadlink.control.model.Function;
import com.broadlink.control.model.FunctionButton;

import java.util.ArrayList;
import java.util.List;

/**
* Created by morfeusys on 01.02.16.
*/
public class BroadlinkUtil {

public static void processFunction(Context context, long id) {
DbHelper helper = new DbHelper(context);
List<FunctionButton> list = helper.getButtons(id);
helper.close();
List<Code> codes = new ArrayList<>();
for (FunctionButton button : list) codes.add(button.getCode());
process(context, codes);
}

public static void processFunction(Context context, String query) {
if (query == null || query.isEmpty()) return;
DbHelper helper = new DbHelper(context);
List<Function> list = helper.queryFunctions(query);
List<Code> codes = new ArrayList<>();
for (Function function : list) {
List<FunctionButton> buttons = helper.getButtons(function.getId());
for (FunctionButton button : buttons) codes.add(button.getCode());
}
helper.close();
process(context, codes);
}

public static void processButton(Context context, String query) {
if (query == null || query.isEmpty()) return;
DbHelper helper = new DbHelper(context);
List<Code> list = helper.queryCodes(query);
helper.close();
process(context, list);
}

private static void process(final Context context, final List<Code> list) {
new Thread(new Runnable() {
@Override
public void run() {
BroadlinkAPI api = initAPI(context);
for (int i = 0; i < list.size(); i++) {
try {
if (i > 0) Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Code code = list.get(i);
if (code.isRm1()) {
api.RM1Send(code.getMac(), code.getData());
} else {
api.RM2Send(code.getMac(), code.getData());
}
}
}
}
}).start();
}

private static BroadlinkAPI initAPI(Context context) {
BroadlinkAPI api = BroadlinkAPI.getInstance(context);
ArrayList<DeviceInfo> devices = api.getProbeList();
if (devices == null) {
api.broadlinkInitNetwork();
try {
Thread.sleep(300);
devices = api.getProbeList();
if (devices != null) {
for (DeviceInfo info : devices) api.addDevice(info);
}
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
return api;
}
}
104 changes: 104 additions & 0 deletions app/src/main/java/com/broadlink/control/ButtonsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.broadlink.control;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.broadlink.control.model.Code;
import com.broadlink.control.model.Function;
import com.broadlink.control.model.FunctionButton;

import java.util.List;

/**
* Created by morfeusys on 01.02.16.
*/
public class ButtonsActivity extends AppCompatActivity {
public static final String FUNCTION_ID = "id";

private DbHelper mDbHelper;
private ArrayAdapter<FunctionButton> mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buttons);
long fid = getIntent().getLongExtra(FUNCTION_ID, 0);
mDbHelper = new DbHelper(this);
Function function = mDbHelper.getFunction(fid);
setTitle(function.getName());
ListView listView = (ListView) findViewById(R.id.list);
List<FunctionButton> list = mDbHelper.getButtons(fid);
mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(mAdapter);
registerForContextMenu(listView);
}

@Override
protected void onDestroy() {
super.onDestroy();
mDbHelper.close();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_add) {
addButton();
}
return true;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.menu_context, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
int position = ((AdapterView.AdapterContextMenuInfo) item.getMenuInfo()).position;
FunctionButton button = mAdapter.getItem(position);
if (item.getItemId() == R.id.remove) {
mAdapter.remove(button);
mAdapter.notifyDataSetChanged();
mDbHelper.removeButton(button.getId());
}
return true;
}

private void addButton() {
final List<Code> list = mDbHelper.getCodes();
CharSequence[] items = new CharSequence[list.size()];
for (int i = 0; i < items.length; i++) {
items[i] = list.get(i).getName();
}
new AlertDialog.Builder(this).setTitle("Выберите кнопку")
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
addButton(list.get(which));
}
}).show();
}

private void addButton(Code code) {
long fid = getIntent().getLongExtra(FUNCTION_ID, 0);
long id = mDbHelper.saveButton(new FunctionButton(0, code), fid);
mAdapter.add(new FunctionButton(id, code));
mAdapter.notifyDataSetChanged();
}
}
Loading

0 comments on commit 47c915f

Please sign in to comment.