Skip to content

Commit

Permalink
setup: IntentReveiver.java: optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck authored and Bambooin committed Aug 11, 2021
1 parent 28fcd87 commit d2edaab
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/src/main/java/com/osfans/trime/ime/core/Trime.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
import com.osfans.trime.Effect;
import com.osfans.trime.Event;
import com.osfans.trime.Function;
import com.osfans.trime.IntentReceiver;
import com.osfans.trime.setup.IntentReceiver;
import com.osfans.trime.Key;
import com.osfans.trime.Keyboard;
import com.osfans.trime.KeyboardSwitch;
Expand Down
73 changes: 73 additions & 0 deletions app/src/main/java/com/osfans/trime/setup/IntentReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2015-present, osfans
* waxaca@163.com https://github.com/osfans
*
* 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 com.osfans.trime.setup;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.osfans.trime.Rime;
import com.osfans.trime.util.RimeUtils;

/** 接收 Intent 廣播事件 */
public class IntentReceiver extends BroadcastReceiver {
private static final String TAG = "IntentReceiver";
private static final String COMMAND_DEPLOY = "com.osfans.trime.deploy";
private static final String COMMAND_SYNC = "com.osfans.trime.sync";

@Override
public void onReceive(@NonNull Context context, @NonNull Intent intent) {
final @Nullable String command = intent.getAction();

Log.d(TAG, "Receive Command = " + command);
//防止为空,虽然很少,但是可能会出现
//http://stackoverflow.com/questions/15048883/intent-getaction-is-returning-null
if (command == null) return;

switch (command) {
case COMMAND_DEPLOY:
RimeUtils.INSTANCE.deploy(context);
System.exit(0);
break;
case COMMAND_SYNC:
RimeUtils.INSTANCE.sync(context);
break;
case Intent.ACTION_SHUTDOWN:
Rime.destroy();
break;
default:
break;
}
}

public void registerReceiver(@NonNull Context context) {
context.registerReceiver(this, new IntentFilter(COMMAND_DEPLOY));
context.registerReceiver(this, new IntentFilter(COMMAND_SYNC));
context.registerReceiver(this, new IntentFilter(Intent.ACTION_SHUTDOWN));
}

public void unregisterReceiver(@NonNull Context context) {
context.unregisterReceiver(this);
}
}

0 comments on commit d2edaab

Please sign in to comment.