Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app-catalog/samples/basic/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@
android:exported="true"
android:theme="@style/Theme.AppCompat.DayNight" />

<activity
android:name=".prejob.uiDemo.ep2_1"
android:exported="true"
android:theme="@style/Theme.AppCompat.DayNight" />

<activity
android:name=".prejob.uiDemo.ep2_2"
android:exported="true"
android:theme="@style/Theme.AppCompat.DayNight" />

<activity
android:name=".job.DbEntryActivity"
android:exported="true"
android:theme="@style/Theme.AppCompat.DayNight" />

<!-- <activity android:name=".ep3_1second"-->
<!-- android:icon="@drawable/rabbit"-->
<!-- android:label="关于"-->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2023-2024 wintmain
*
* 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
*
* https://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.
*/

package com.wintmain.basic.job

import android.os.Bundle
import android.os.PersistableBundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.catalog.framework.annotations.Sample
import com.wintmain.basic.R

@Sample(
name = "数据库示例",
description = "数据库的创建添加",
documentation = "",
tags = ["A-Self_demos"],
)
class DbEntryActivity : AppCompatActivity(R.layout.activity_db_entry) {
private lateinit var dbHelper: DbHelper

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Initialize DbHelper
dbHelper = DbHelper(applicationContext)
dbHelper.writableDatabase // Ensure database is created
addlisteners()
}

private fun addlisteners() {
findViewById<Button>(R.id.button_create).setOnClickListener {
// You may not need to do anything here as the database is already created in onCreate
Toast.makeText(applicationContext, "Create success.", Toast.LENGTH_LONG).show()
}
findViewById<Button>(R.id.button_add).setOnClickListener {
DbHelper.addToWhitelist(this, "com.wintmain.catalog.app")
Toast.makeText(this, "Add success.", Toast.LENGTH_LONG).show()
}
findViewById<Button>(R.id.button_remove).setOnClickListener {
DbHelper.removeFromWhitelist(this, "com.wintmain.catalog.app")
Toast.makeText(this, "Remove success.", Toast.LENGTH_LONG).show()
}
findViewById<Button>(R.id.button_getall).setOnClickListener {
DbHelper.addToWhitelist(this, "com.wintmain.catalog.app1")
DbHelper.addToWhitelist(this, "com.wintmain.catalog.app2")
DbHelper.addToWhitelist(this, "com.wintmain.catalog.app3")
val listall = DbHelper.getWhitelist(this)
Toast.makeText(this, "GetAll success: $listall", Toast.LENGTH_LONG).show()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2023-2024 wintmain
*
* 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
*
* https://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.
*/

package com.wintmain.basic.job;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

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

public class DbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "demo_whitelist.db";
private static final int DATABASE_VERSION = 1;

public static class WhitelistEntry implements BaseColumns {
public static final String TABLE_NAME = "whitelist";
public static final String COLUMN_PACKAGE_NAME = "package_name";
}

private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + WhitelistEntry.TABLE_NAME
+ " (" + WhitelistEntry._ID + " INTEGER PRIMARY KEY," + WhitelistEntry.COLUMN_PACKAGE_NAME
+ " TEXT)";
private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + WhitelistEntry.TABLE_NAME;

public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
Log.d("DbHelper", "Database created");
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}

public static void addToWhitelist(Context context, String packageName) {
DbHelper dbHelper = new DbHelper(context);
SQLiteDatabase db = null;
try {
db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(WhitelistEntry.COLUMN_PACKAGE_NAME, packageName);
db.insert(WhitelistEntry.TABLE_NAME, null, values);
} catch (Exception e) {
Log.e("DbHelper", "Error adding to whitelist", e);
} finally {
if (db != null) {
db.close();
}
}
}

// 从白名单列表中移除包名
public static void removeFromWhitelist(Context context, String packageName) {
DbHelper dbHelper = new DbHelper(context);
SQLiteDatabase db = dbHelper.getWritableDatabase();

String selection = WhitelistEntry.COLUMN_PACKAGE_NAME + " = ?";
String[] selectionArgs = { packageName };

db.delete(WhitelistEntry.TABLE_NAME, selection, selectionArgs);
db.close();
}

// 获取白名单列表
public static List<String> getWhitelist(Context context) {
DbHelper dbHelper = new DbHelper(context);
SQLiteDatabase db = dbHelper.getReadableDatabase();

String[] projection = { WhitelistEntry.COLUMN_PACKAGE_NAME };

Cursor cursor = db.query(
WhitelistEntry.TABLE_NAME,
projection,
null,
null,
null,
null,
null
);

List<String> whitelist = new ArrayList<>();
while (cursor.moveToNext()) {
String packageName = cursor.getString(cursor.getColumnIndexOrThrow(
WhitelistEntry.COLUMN_PACKAGE_NAME));
whitelist.add(packageName);
}

cursor.close();
db.close();

return whitelist;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2023-2024 wintmain
*
* 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
*
* https://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.
*/

package com.wintmain.basic.prejob.uiDemo

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.View
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.Button
import android.widget.ProgressBar
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.catalog.framework.annotations.Sample
import com.wintmain.basic.R


@Sample(
name = "ArrayAdapter",
description = "Adapter适配器入门",
documentation = "",
tags = ["A-Self_demos"],
)
class ep2_1 : AppCompatActivity() {
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.example2_1)

// 获取自动完成文本框
val textView =
findViewById<View>(R.id.autoCompleteTextView1) as AutoCompleteTextView
// 创建一个ArrayAdapter适配器
val adapter =
ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES)
// 为自动完成文本框设置适配器
textView.setAdapter(adapter)

val button = findViewById<View>(R.id.button1) as Button
button.setOnClickListener {
Toast.makeText(
this@ep2_1,
textView.text.toString(),
Toast.LENGTH_SHORT
).show()
}
}

companion object {
private val COUNTRIES =
arrayOf("android", "android app", "android开发", "开发应用", "开发者")
}
}

@Sample(
name = "进度条",
description = "进度条展示入门",
documentation = "",
tags = ["A-Self_demos"],
)
class ep2_2 : AppCompatActivity() {
private var horizonP: ProgressBar? = null // 水平进度条
private var circleP: ProgressBar? = null // 圆形进度条
private var mProgressStatus = 0 // 完成进度
private var mHandler: Handler // 声明一个用于处理消息的Handler类的对象

init {
// 使用主线程的 Looper 创建 Handler
mHandler = Handler(Looper.getMainLooper()) {
when (it.what) {
0x111 -> {
horizonP!!.progress = mProgressStatus // 更新进度
}
0x110 -> {
Toast.makeText(this@ep2_2, "耗时操作已经完成", Toast.LENGTH_SHORT).show()
horizonP!!.visibility = View.GONE // 设置进度条不显示,并且不占用空间
circleP!!.visibility = View.GONE
}
}
true // 表示消息被处理
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.example2_2)

horizonP = findViewById(R.id.progressBar1) // 获取进度条
circleP = findViewById(R.id.progressBar2)

Thread {
while (true) {
mProgressStatus = doWork() // 获取耗时操作完成的百分比
val m = Message()
if (mProgressStatus < 100) {
m.what = 0x111
mHandler.sendMessage(m) // 发送信息
} else {
m.what = 0x110
mHandler.sendMessage(m) // 发送消息
break
}
}
}.start() // 开启一个线程
}

// 模拟一个耗时操作
private fun doWork(): Int {
mProgressStatus = (mProgressStatus + (Math.random() * 10)).toInt() // 改变完成进度
try {
Thread.sleep(200) // 线程休眠200毫秒
} catch (e: InterruptedException) {
e.printStackTrace()
}
return mProgressStatus
}
}
Loading
Loading