-
-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setup:
IntentReveiver.java
: optimization
- Loading branch information
1 parent
28fcd87
commit d2edaab
Showing
2 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
app/src/main/java/com/osfans/trime/setup/IntentReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |