Browse Source

增加 KeyboardIME 服务

liuyuqi-dellpc 3 years ago
parent
commit
9a4d662b4a

+ 7 - 0
README.md

@@ -0,0 +1,7 @@
+# SafeKeyboard
+
+android safe keybodard.
+
+
+
+

+ 12 - 0
app/src/main/AndroidManifest.xml

@@ -5,6 +5,7 @@
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
+    <uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>
 
     <application
         android:allowBackup="true"
@@ -20,6 +21,17 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+        <service
+            android:name=".service.KeyboardIME"
+            android:label="@string/app_name"
+            android:permission="android.permission.BIND_INPUT_METHOD">
+            <intent-filter>
+                <action android:name="android.view.InputMethod" />
+            </intent-filter>
+            <meta-data
+                android:name="android.view.im"
+                android:resource="@xml/method" />
+        </service>
     </application>
 
 </manifest>

+ 31 - 0
app/src/main/java/me/yoqi/android/safekeyboard/MainActivity.java

@@ -1,7 +1,12 @@
 package me.yoqi.android.safekeyboard;
 
+import android.content.Context;
+import android.content.Intent;
 import android.os.Bundle;
+import android.provider.Settings;
 import android.view.View;
+import android.view.inputmethod.InputMethodManager;
+import android.widget.Button;
 import android.widget.EditText;
 
 import androidx.appcompat.app.AppCompatActivity;
@@ -11,11 +16,23 @@ import me.yoqi.android.safekeyboard.keyboard.KeyBoardDialogUtils;
 public class MainActivity extends AppCompatActivity {
     private KeyBoardDialogUtils keyBoardDialogUtils;
     private EditText et;
+    private Button btnChangeIME;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
+        initView();
+    }
+
+    public void initView() {
+        btnChangeIME = findViewById(R.id.btnChangeIME);
+        btnChangeIME.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+               changeIME();
+            }
+        });
 
         et = (EditText) findViewById(R.id.et);
         keyBoardDialogUtils = new KeyBoardDialogUtils(this);
@@ -26,4 +43,18 @@ public class MainActivity extends AppCompatActivity {
             }
         });
     }
+
+    public void changeIME() {
+        //1、判断是否系统启用了安全输入法没有启动者跳到设置界面
+        if (true) {
+            Intent enableIntent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
+            enableIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            startActivity(enableIntent);
+        } else if (false) {
+            // 2、如果设置了安全输入法,但是没有启动,则跳转到切换输入法界面:
+            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
+            imm.showInputMethodPicker();
+        }
+    }
+
 }

+ 7 - 9
app/src/main/java/me/yoqi/android/safekeyboard/keyboard/CustomKeyboardView.java → app/src/main/java/me/yoqi/android/safekeyboard/keyboard/view/CustomKeyboardView.java

@@ -1,4 +1,4 @@
-package me.yoqi.android.safekeyboard.keyboard;
+package me.yoqi.android.safekeyboard.keyboard.view;
 
 import android.content.Context;
 import android.graphics.Canvas;
@@ -15,8 +15,9 @@ import java.lang.reflect.Field;
 import java.util.List;
 
 import me.yoqi.android.safekeyboard.R;
+import me.yoqi.android.safekeyboard.keyboard.KhKeyboardView;
 
-/** 自定义全键盘布局
+/** 自定义全键盘布局 继承系统的 KeyboardView
  * Created by liuyu1 on 2017/8/2.
  */
 
@@ -27,6 +28,9 @@ public class CustomKeyboardView extends KeyboardView {
         this.context = context;
     }
 
+    /** 绘制一个键盘
+     * @param canvas
+     */
     @Override
     public void onDraw(Canvas canvas) {
         super.onDraw(canvas);
@@ -69,20 +73,15 @@ public class CustomKeyboardView extends KeyboardView {
 
     private void drawText(Canvas canvas, Keyboard.Key key) {
         try {
-            Rect bounds = new Rect();
+            Rect bounds = new Rect(); //矩形
             Paint paint = new Paint();
             paint.setTextAlign(Paint.Align.CENTER);
-
-
             paint.setAntiAlias(true);
-
             paint.setColor(Color.WHITE);
 
             if (key.label != null) {
                 String label = key.label.toString();
-
                 Field field;
-
                 if (label.length() > 1 && key.codes.length < 2) {
                     int labelTextSize = 0;
                     try {
@@ -110,7 +109,6 @@ public class CustomKeyboardView extends KeyboardView {
                     paint.setTextSize(keyTextSize);
                     paint.setTypeface(Typeface.DEFAULT);
                 }
-
                 paint.getTextBounds(key.label.toString(), 0, key.label.toString()
                         .length(), bounds);
                 canvas.drawText(key.label.toString(), key.x + (key.width / 2),

+ 81 - 0
app/src/main/java/me/yoqi/android/safekeyboard/service/KeyboardIME.java

@@ -0,0 +1,81 @@
+package me.yoqi.android.safekeyboard.service;
+
+import android.content.Context;
+import android.inputmethodservice.InputMethodService;
+import android.os.PowerManager;
+import android.view.View;
+import android.view.inputmethod.EditorInfo;
+import android.widget.TextView;
+
+import me.yoqi.android.safekeyboard.R;
+
+/**
+ * 自定义键盘服务
+ *
+ * @author liuyuqi.gov@msn.cn
+ * @date 3/16/2021
+ */
+public class KeyboardIME extends InputMethodService {
+    private static final String TAG = "KeyboardIME";
+    private TextView mTextViewMessage;
+    private PowerManager.WakeLock mWakeLock;
+    Context mContext;
+
+    private void screenLock() {
+        if (mWakeLock != null && !mWakeLock.isHeld()) { //设置10分钟后锁屏
+            mWakeLock.acquire(10 * 60 * 1000L /*10 minutes*/);
+            return;
+        }
+        mWakeLock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "everettjf:remoboard");
+        if (mWakeLock != null) {
+            mWakeLock.acquire(10 * 60 * 1000L /*10 minutes*/);
+        }
+    }
+
+    private void screenUnlock() {
+        if (mWakeLock != null) {
+            mWakeLock.release();
+            mWakeLock = null;
+        }
+    }
+
+    /**
+     * 设置相应控件的事件,如软键盘按钮单击事件
+     *
+     * @return 返回建立的布局文件对应的View对象
+     */
+    @Override
+    public View onCreateInputView() {
+        mContext = this;
+        return getLayoutInflater().inflate(R.layout.keyboard_key_board_popu, null);
+    }
+
+    @Override
+    public void onStartInputView(EditorInfo info, boolean restarting) {
+        super.onStartInputView(info, restarting);
+    }
+
+    @Override
+    public void onStartInput(EditorInfo attribute, boolean restarting) {
+        super.onStartInput(attribute, restarting);
+    }
+
+    @Override
+    public void onFinishInput() {
+        super.onFinishInput();
+        screenUnlock();
+    }
+
+    @Override
+    public boolean onEvaluateFullscreenMode() {
+        return super.onEvaluateFullscreenMode();
+    }
+
+    @Override
+    public void updateInputViewShown() {
+        super.updateInputViewShown();
+        if (isInputViewShown()) {
+            screenLock();
+        }
+    }
+}

+ 11 - 0
app/src/main/res/layout/activity_main.xml

@@ -6,6 +6,17 @@
     android:layout_height="match_parent"
     tools:context=".MainActivity">
 
+    <Button
+        android:id="@+id/btnChangeIME"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="51dp"
+        android:layout_marginLeft="51dp"
+        android:layout_marginTop="134dp"
+        android:text="切换输入法"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent"></Button>
+
     <EditText
         android:id="@+id/et"
         android:layout_width="200dp"

+ 2 - 2
app/src/main/res/layout/keyboard_key_board_popu.xml

@@ -56,7 +56,7 @@
             android:layout_width="match_parent"
             android:layout_height="wrap_content">
 
-            <me.yoqi.android.safekeyboard.keyboard.CustomKeyboardView
+            <me.yoqi.android.safekeyboard.keyboard.view.CustomKeyboardView
                 android:id="@+id/keyboard_view"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
@@ -68,7 +68,7 @@
                 android:keyTextColor="#ffffff"
                 android:visibility="gone" />
 
-            <me.yoqi.android.safekeyboard.keyboard.CustomKeyboardView
+            <me.yoqi.android.safekeyboard.keyboard.view.CustomKeyboardView
                 android:id="@+id/keyboard_view_2"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"

+ 19 - 0
app/src/main/res/xml/method.xml

@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/**
+ * Copyright (c) 2016, The Android Open Source Project
+ *
+ * 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.
+ */
+-->
+<input-method xmlns:android="http://schemas.android.com/apk/res/android" />