updated android-example to 1.5.0 latest
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||
@@ -15,12 +14,14 @@
|
||||
android:icon="@mipmap/ir_logo"
|
||||
android:label="@string/app_name"
|
||||
android:supportsRtl="true"
|
||||
android:requestLegacyExternalStorage="true"
|
||||
android:networkSecurityConfig="@xml/network_security_config"
|
||||
android:theme="@android:style/Theme.Holo.Light.DarkActionBar">
|
||||
|
||||
<activity
|
||||
android:name="net.irext.ircontrol.ui.activity.MainActivity"
|
||||
android:label="@string/title_activity_main"
|
||||
android:exported="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
|
||||
@@ -71,4 +71,8 @@ public class IRApplication extends com.activeandroid.app.Application {
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
public static String getApplicationName() {
|
||||
return IRApplication.class.getPackageName();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,16 +11,20 @@ import android.support.annotation.NonNull;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
import net.irext.ircontrol.R;
|
||||
import net.irext.ircontrol.bean.RemoteControl;
|
||||
import net.irext.ircontrol.ui.fragment.MainFragment;
|
||||
import net.irext.ircontrol.utils.MessageUtil;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Filename: MainActivity.java
|
||||
@@ -37,6 +41,11 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private static final String TAG = MainActivity.class.getSimpleName();
|
||||
|
||||
private boolean mReadPermissionGranted = false;
|
||||
|
||||
private boolean mWritePermissionGranted = false;
|
||||
private static final int PERMISSIONS_REQUEST_STORAGE = 1001;
|
||||
|
||||
public static final int CMD_GOTO_CONTROL = 0;
|
||||
|
||||
private RemoteControl mCurrentRemoteControl;
|
||||
@@ -62,6 +71,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
initView();
|
||||
checkAndRequestStoragePermissions();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -98,49 +108,64 @@ public class MainActivity extends AppCompatActivity {
|
||||
gotoCreateNew();
|
||||
}
|
||||
});
|
||||
isReadStoragePermissionGranted();
|
||||
isWriteStoragePermissionGranted();
|
||||
}
|
||||
|
||||
public void isReadStoragePermissionGranted() {
|
||||
if (Build.VERSION.SDK_INT >= 23) {
|
||||
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||
== PackageManager.PERMISSION_GRANTED) {
|
||||
Log.v(TAG,"read permission is granted");
|
||||
} else {
|
||||
private void checkAndRequestStoragePermissions() {
|
||||
boolean readGranted = ContextCompat.checkSelfPermission(this,
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
|
||||
boolean writeGranted = ContextCompat.checkSelfPermission(this,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
|
||||
|
||||
Log.v(TAG,"read permission is revoked");
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 3);
|
||||
}
|
||||
List<String> permissionsToRequest = new ArrayList<>();
|
||||
|
||||
if (!readGranted) {
|
||||
permissionsToRequest.add(Manifest.permission.READ_EXTERNAL_STORAGE);
|
||||
Log.w(TAG, "read external storage permission not granted");
|
||||
}
|
||||
else { //permission is automatically granted on sdk<23 upon installation
|
||||
Log.v(TAG,"read permission is granted");
|
||||
|
||||
if (!writeGranted) {
|
||||
permissionsToRequest.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
||||
Log.w(TAG, "write external storage permission not granted");
|
||||
}
|
||||
|
||||
if (!permissionsToRequest.isEmpty()) {
|
||||
Log.d(TAG, "requesting permission: " + String.join(", ", permissionsToRequest));
|
||||
ActivityCompat.requestPermissions(this,
|
||||
permissionsToRequest.toArray(new String[0]),
|
||||
PERMISSIONS_REQUEST_STORAGE);
|
||||
} else {
|
||||
Log.i(TAG, "storage permissions already granted.");
|
||||
mReadPermissionGranted = true;
|
||||
mWritePermissionGranted = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void isWriteStoragePermissionGranted() {
|
||||
if (Build.VERSION.SDK_INT >= 23) {
|
||||
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||
== PackageManager.PERMISSION_GRANTED) {
|
||||
Log.v(TAG,"write permission is granted");
|
||||
} else {
|
||||
|
||||
Log.v(TAG,"write permission is revoked");
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// permission is automatically granted on sdk<23 upon installation
|
||||
Log.v(TAG,"write permission is granted");
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == PERMISSIONS_REQUEST_STORAGE) {
|
||||
Log.d(TAG, "storage permission requested successfully");
|
||||
mReadPermissionGranted = true;
|
||||
mWritePermissionGranted = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void gotoCreateNew() {
|
||||
if (!mReadPermissionGranted || !mWritePermissionGranted) {
|
||||
Log.e(TAG, "storage read and write permission not granted");
|
||||
Toast.makeText(this, this.getString(R.string.storage_permission_warning), Toast.LENGTH_SHORT).show();
|
||||
checkAndRequestStoragePermissions();
|
||||
}
|
||||
Intent intent = new Intent(this, CreateActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
private void gotoControl() {
|
||||
if (!mReadPermissionGranted || !mWritePermissionGranted) {
|
||||
Log.e(TAG, "storage read and write permission not granted");
|
||||
Toast.makeText(this, this.getString(R.string.storage_permission_warning), Toast.LENGTH_SHORT).show();
|
||||
checkAndRequestStoragePermissions();
|
||||
}
|
||||
Intent intent = new Intent(this, ControlActivity.class);
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putLong(ControlActivity.KEY_REMOTE_ID, mCurrentRemoteControl.getID());
|
||||
@@ -167,10 +192,4 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
|
||||
@NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
||||
mCurrentRemoteControl = RemoteControl.getRemoteControl(mRemoteID);
|
||||
if (null != mCurrentRemoteControl) {
|
||||
int category = mCurrentRemoteControl.getCategoryId();
|
||||
String binFileName = FileUtils.BIN_PATH + FileUtils.FILE_NAME_PREFIX +
|
||||
String binFileName = FileUtils.binDir + FileUtils.FILE_NAME_PREFIX +
|
||||
mCurrentRemoteControl.getRemoteMap() + FileUtils.FILE_NAME_EXT;
|
||||
|
||||
/* decode SDK - load binary file */
|
||||
@@ -234,6 +234,14 @@ public class ControlFragment extends Fragment implements View.OnClickListener {
|
||||
} else if (id == R.id.iv_menu) {
|
||||
decoded = irControl(KEY_MENU);
|
||||
}
|
||||
|
||||
// debug decoded value
|
||||
String decodedValue = "";
|
||||
for (int i = 0; i < decoded.length; i++) {
|
||||
decodedValue += decoded[i];
|
||||
decodedValue += ",";
|
||||
}
|
||||
Log.d(TAG, "decodedValue : " + decodedValue);
|
||||
// send decoded integer array to IR emitter
|
||||
ConsumerIrManager irEmitter =
|
||||
(ConsumerIrManager) mParent.getSystemService(Context.CONSUMER_IR_SERVICE);
|
||||
|
||||
@@ -145,10 +145,11 @@ public class IndexFragment extends BaseCreateFragment {
|
||||
}
|
||||
|
||||
private boolean createDirectory() {
|
||||
File file = new File(FileUtils.BIN_PATH);
|
||||
File file = new File(FileUtils.binDir);
|
||||
if (file.exists()) {
|
||||
return true;
|
||||
}
|
||||
Log.d(TAG, "create directory : " + file.getAbsolutePath());
|
||||
return file.mkdirs();
|
||||
}
|
||||
|
||||
@@ -157,7 +158,7 @@ public class IndexFragment extends BaseCreateFragment {
|
||||
@Override
|
||||
public void run() {
|
||||
if (createDirectory()) {
|
||||
File binFile = new File(FileUtils.BIN_PATH +
|
||||
File binFile = new File(FileUtils.binDir +
|
||||
FileUtils.FILE_NAME_PREFIX + mCurrentIndex.getRemoteMap() +
|
||||
FileUtils.FILE_NAME_EXT);
|
||||
boolean ret = FileUtils.write(binFile, mBinStream);
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.irext.ircontrol.utils;
|
||||
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
import net.irext.ircontrol.IRApplication;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
@@ -20,9 +21,9 @@ public class FileUtils {
|
||||
|
||||
private static final String TAG = FileUtils.class.getSimpleName();
|
||||
|
||||
public static final String BASE_PATH = Environment.getExternalStorageDirectory() + File.separator +
|
||||
"irext" + File.separator;
|
||||
public static final String BIN_PATH = BASE_PATH + "bin" + File.separator;
|
||||
public static final String packageDataDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +
|
||||
Environment.DIRECTORY_DOWNLOADS + File.separator + IRApplication.getApplicationName() + File.separator;
|
||||
public static final String binDir = packageDataDir + "bin" + File.separator;
|
||||
|
||||
public static final String FILE_NAME_PREFIX = "irext_";
|
||||
public static final String FILE_NAME_EXT = ".ir";
|
||||
@@ -64,36 +65,15 @@ public class FileUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static File createBinaryFile(String fileName) {
|
||||
if(createDirs(BASE_PATH) && createDirs(BIN_PATH)) {
|
||||
String path = BIN_PATH + fileName;
|
||||
File file = new File(path);
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
if (file.createNewFile()) {
|
||||
return file;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
Log.w(TAG, "binary file already exists ?");
|
||||
}
|
||||
} else {
|
||||
Log.e(TAG, "failed to create dirs");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean createDirs(String path) {
|
||||
File file = new File(path);
|
||||
return file.exists() || file.mkdir();
|
||||
}
|
||||
|
||||
public static File getLocalFile(String fileName) {
|
||||
File file = new File(BIN_PATH);
|
||||
File file = new File(binDir);
|
||||
if (file.exists()) {
|
||||
String path = BIN_PATH + fileName;
|
||||
String path = binDir + fileName;
|
||||
file = new File(path);
|
||||
if (file.exists()) {
|
||||
return file;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package net.irext.iris.utils;
|
||||
package net.irext.ircontrol.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.widget.Toast;
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
Before Width: | Height: | Size: 951 B |
Binary file not shown.
|
Before Width: | Height: | Size: 661 B |
Binary file not shown.
|
Before Width: | Height: | Size: 1.2 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 2.3 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 2.8 KiB |
@@ -11,6 +11,7 @@
|
||||
|
||||
<!-- Main UI -->
|
||||
<string name="create_new">点击 + 安装遥控</string>
|
||||
<string name="storage_permission_warning">请先授权应用对存储进行读写</string>
|
||||
|
||||
<!-- Create New -->
|
||||
<string name="default_category">电视机</string>
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
<!-- Main UI -->
|
||||
<string name="create_new">Tap + to create new remote control</string>
|
||||
<string name="storage_permission_warning">Please grant storage read and write permission first</string>
|
||||
|
||||
<!-- Create New -->
|
||||
<string name="default_category">TV</string>
|
||||
|
||||
Reference in New Issue
Block a user