android Git文字筆記APP更新版本

3年前我實作一個Git文字筆記app, 使用jgit套件, 可將github clone到android手機上, 然後進行編輯, 編輯完畢,直接版控到github上面,

最近play store要求target api在2022年11月前, 至少要設定31 , 於是我就編輯了一下, 實在有夠麻煩, 一大堆套件要升級, android studio也是大改版要求舊的專案也要跟著升級.

升級升級, 想說之前jgit只使用4.5.7版本, 趁這機會,改用jgit 6.3.x版, 後來發現6.3.x 使用outputstream類別中的一個函式”transferTo”, 造成android app 找不到該method,程式跳掉.

最後我發現 android 13才支援”transferTo”

另外我開發的這支app, 當初硬改成可以接受未正式簽章的SSL網站, 現在變成資安問題,上架失敗, 原因是X509相關類別,被我繼承改成一律安全 , 只好再改成原本的檢查方式,

也就是說以後這支app要使用遠端git網站, 必須是正式簽章HTTPS, 否則是不能使用的.


為何我的app要使用jgit 6.3.x , 因為jgit終於支援shallow(depth)功能, 可以讓我clone的時候快一點, 不然有些commit數量太多, 要clone很久,很麻煩, 而app本身只想用來做文字筆記, 不用clone太多commit版本下來,不然使用者會嫌棄的.

公司有pixel手機, 趕緊升級到android 13

android 檢查play store是否安裝

java

public static boolean isPlayStoreInstalled(Context context){
    try {
        context.getPackageManager()
                .getPackageInfo(GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE, 0);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

kotlin

fun isPlayStoreInstalled(context: Context): Boolean {
    return try {
        context.packageManager
            .getPackageInfo(GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE, 0)
        true
    } catch (e: PackageManager.NameNotFoundException) {
        false
    }
}

build.gradle

    implementation 'com.google.android.gms:play-services-base:18.0.1'

android app關於新版本更新的想法

android app 新版本更新,除了google play store之外, 有些不含play store的, 該怎麼做?

簡單的方式就是使用自動更新套件, 搭配自己手動維護版本訊息, app就能自行更新, 但是該套件需要”安裝權限”, 有很大機率會被拒絕, 當然現在也不能上架play store , 除非說明原因.

app也可以自行檢查有新版本(自己手動維護版本訊息), 若有,也可以導到play store或是自己的網頁下載apk程式自行安裝.


val appPackageName =  getPackageName()
val intent = Intent(Intent.ACTION_VIEW).apply {
  data = Uri.parse("https://play.google.com/store/apps/details?id=$appPackageName")
  setPackage("com.android.vending")
}
startActivity(intent)

另外若app可架play store成功, 可以額外下載play store簽名的apk, 使用該apk, 就可以避開簽名者不明的警告訊息呢.

android app字型不隨系統字型大小起舞

重點有二

  1. 在activity set content之前寫死字型大小
  2. 字型大小: resource的fontscale數值
  val resources: Resources = myactivity.getResources()
  var fontScale:Float = 1.0f
  if (resources != null && resources.getConfiguration().fontScale !== fontScale) {
    val configuration: Configuration = resources.getConfiguration()
    configuration.fontScale = fontScale
    resources.updateConfiguration(configuration, resources.getDisplayMetrics())
  }
 // 在set content之前
 setContent {
 }

android強制停用深色模式(Dark Theme)

android 10開始,已經支援Dark Them深色模式, 但程式若沒有調整好, 就會出現有些字憑空消失,或是圖片失真問題,

因此必須修改程式, 先強制停用深色模式, 再思考人生.

  • Activity 加上以下程式 (若使用 AppCompat v1.1.0 ,可以不用加)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
  • Activity的theme設定,增加 ( theme.xml 或是 style.xml )
   <style name="MyAppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
    </style>
 <activity
    android:name=".MainActivity"
	...
    android:theme="@style/AppTheme" >
	...
 </activity>

android 10 藍牙程式必須取得ACCESS_FINE_LOCATION權限

寫android 藍牙程式, 若需要scan 功能, 在android 10以前, 只要取得ACCESS_COARSE_LOCATION權限即可, 到了android 10則必須取得ACCESS_FINE_LOCATION權限

詳細的寫法就不多著墨了

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

v2ray初體驗-使用v2-ui初階安裝

安裝v2ray之前,你必須符合以下基本技術基礎

  1. 會安裝CEntOS 7
  2. 懂apache httpd 2.4基本設定
  3. 懂dns, 懂申請let’s Encrypt

你也必須準備以下軟硬體

  1. 安裝好CEntOS7
  2. 有Internet IP
  3. 有網域,並可賦予該系統一個網路名稱

開始安裝

1. 安裝v2ray
到這個神人網站 https://github.com/sprov065/v2-ui/
一鍵安裝
bash <(curl -Ls https://blog.sprov.xyz/v2-ui.sh)

2. 安裝完畢網站預設是 http://ip:65432 預設帳號密碼都是admin , 記得登入修改密碼


3. 以下示範兩種連線, 一種http/2(h2), 另一種透過apache httpd(port 443)代理連到後端websocket
3.1 設定 h2 連線
Server:


client:
v2ray初體驗-使用v2-ui初階安裝



3.2
Server 設定:


Clinet 設定:
websocket , port 用443, 要輸入tls

apache httpd 設定:

ServerName xxx
ErrorLog /var/log/httpd/ssl_error_log1
TransferLog /var/log/httpd/ssl_access_log1

SSLEngine On
SSLProxyEngine On
SSLCertificateFile /etc/letsencrypt/live/xxx/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/xxx/privkey.pem
SSLCACertificateFile /etc/letsencrypt/live/xxx/fullchain.pem
## v2-ui management
RewriteEngine On
ProxyPass / http://localhost:65432/
ProxyPassReverse / http://localhost:65432/
## server
RewriteRule /ray/(.*) ws://localhost:1234/$1 [P,L]
RewriteCond %{HTTP:Upgrade} =websocket [NC]
ProxyPass /ray/ http://localhost:1234/
ProxyPassReverse /ray/ http://localhost:1234/


4. 心得
若是使用3.1 , 使用httpd/2的方式
若是使用3.2可以看起來像是https的存取

5. 額外指令

 v2-ui              - 显示管理菜单 (功能更多)
 v2-ui start        - 启动 v2-ui 面板
 v2-ui stop         - 停止 v2-ui 面板
 v2-ui restart      - 重启 v2-ui 面板
 v2-ui status       - 查看 v2-ui 状态
 v2-ui enable       - 设置 v2-ui 开机自启
 v2-ui disable      - 取消 v2-ui 开机自启
 v2-ui log          - 查看 v2-ui 日志
 v2-ui update       - 更新 v2-ui 面板
 v2-ui install      - 安装 v2-ui 面板
 v2-ui uninstall    - 卸载 v2-ui 面板

Android 畫面內容更新注意事項

通常畫面更新前, 需要背景處理的工作, 有時候會耗掉一些時間, 若使用

runOnUIThread 會造成畫面停頓,好像當掉一樣, 此時可以使用AsyncTask處理

class MyAsyncTask extends AsyncTask<Void, Void, Void>{
   
    @Override
    protected Void doInBackground(Void... params) {
    // 這裡處理背景工作
      return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
// 這裡處理畫面元件(這邊會耗時,所以盡量只處理簡單的畫面資料
    }
}
1 2 3