Android PDFの編集・保存・印刷その後2

すがすがしい顔をしています。まだ朝の内ですので元気な顔をしている柴犬です。
概要
5月4日投稿の「Android PDFの編集・保存・印刷その後」で、プリンタの検索をしましたが、起動するたびに検索するのは無駄ですので、検索結果を保存できないか考えてみました。
翔泳社 齊藤 新三 著
2024年2月26日現在
WEBのみでは断片的で覚えにくいので最初に購入した Kotlin の本です。
basicData
SharedPreferences を使ってアプリのストレージに保存することにしました。
保存フォルダーのパス /data/data/[パッケージ名]/shared_prefs/
ファイル名 [コンストラクタの引数 String name].xml
Class を作ってみました。
private class basicData {
SharedPreferences _data;
SharedPreferences.Editor _editor;
public basicData(String name) {
_data = getSharedPreferences(name, Context.MODE_PRIVATE);
_editor = _data.edit();
}
public void put(String key, String value) {
this._editor.putString(key, value);
this._editor.apply();
}
public String get(String key) {
return this._data.getString(key, "");
}
public String remove(String key) {
String temp = this.get(key);
this._editor.remove(key);
return temp;
}
public String clear() {
String temp = this.toJsonString();
this._editor.clear();
this.put("プリンタ数","0");
return temp;
}
public Boolean exists(String key) {
return this._data.getString(key, "nokey") != "nokey";
}
public SharedPreferences.Editor editor() {
return _editor;
}
public final String toJsonString() {
JSONObject preferencesJson = new JSONObject();
Map<String, ?> map = this._data.getAll();
try {
for (Map.Entry<String, ?> entry : map.entrySet()) {
preferencesJson.put(entry.getKey(), entry.getValue());
}
} catch (JSONException e) {
e.printStackTrace();
}
return preferencesJson.toString();
}
public final String toString(Context context) {
return this._data.getAll().toString();
}
}
printerSearch
for (Channel channel : channels.getChannels()) {} 周りのところでインスタンスを使いました。
private void printerSearch() {
//クラス printerSearchTask の実体を作成
printerSearchTask backgroundReceiver = new printerSearchTask(this);
//executorService を作成
ExecutorService executorService = Executors.newSingleThreadExecutor();
//executorService.submit の引数に実体を渡します
//バックグラウンドで非同期処理が行われ戻り値が future に格納されます
Future<PrinterSearchResult> future;
future = executorService.submit(backgroundReceiver);
PrinterSearchResult channels;
try {
//future から channels の配列を取り出します
channels = future.get();
if (channels != null) {
TextView[] tvlist = {findViewById(R.id.textView01),
findViewById(R.id.textView02),
findViewById(R.id.textView03)};
String modelname = "";
String ipaddress = "";
int i = 0;
String[] printerList = new String[channels.getChannels().size()];
bD.clear();
for (Channel channel : channels.getChannels()) {
modelname = channel.getExtraInfo().get(Channel.ExtraInfoKey.ModelName);
ipaddress = channel.getChannelInfo();
printerList[i] = "プリンタ名: " + modelname + "\n" +
"IPアドレス: " + ipaddress;
bD.put("プリンタ名_" + i, modelname);
bD.put("IPアドレス_" + i, ipaddress);
if (i < tvlist.length ) {
tvlist[i].setText(printerList[i]);
}
i += 1;
}
bD.put("プリンタ数", String.valueOf(i));
TextView tv;
tv = findViewById(R.id.tvText1);
if (printerList.length > 0) {
tv.setText(String.join("\n", printerList));
} else {
tv.setText("使用できるプリンタはありません");
}
}
}
catch(ExecutionException | InterruptedException ex) {
Log.w("printerSearch catch", "非同期処理結果の取得で例外発生: ", ex);
}
finally {
Log.d("printerSearch finally", "非同期処理完了");
executorService.shutdown();
}
}
onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ClickListener cl = new ClickListener();
TextView tv;
tv = findViewById(R.id.textView01);
tv.setOnClickListener(cl);
tv = findViewById(R.id.textView02);
tv.setOnClickListener(cl);
tv = findViewById(R.id.textView03);
tv.setOnClickListener(cl);
tvTellop1 = findViewById(R.id.tvTellop1);
tvTellop2 = findViewById(R.id.tvTellop2);
tvTellop3 = findViewById(R.id.tvTellop3);
etInput1 = findViewById(R.id.etInput1);
etInput2 = findViewById(R.id.etInput2);
etInput3 = findViewById(R.id.etInput3);
ImageView im = findViewById(R.id.image);
im.setBackgroundResource(R.drawable.border);
Button btn;
btn =findViewById(R.id.button);
btn.setOnClickListener(cl);
Log.d("onCreate1","cl");
// ここでインスタンスを作ります
bD = new basicData("printerIpAddress");
// ダミーデータです
bD.put("プリンタ名_0","model00");
bD.put("プリンタ名_1","model11");
bD.put("プリンタ名_2","model22");
bD.put("IPアドレス_0","111.222.1.1");
bD.put("IPアドレス_1","111.222.2.2");
bD.put("IPアドレス_2","111.222.3.3");
bD.put("プリンタ数","3");
String temp = bD.toJsonString();
TextView tvv;
tvv = findViewById(R.id.tvText1);
tvv.setText(temp);
Log.d("onCreate1",temp);
// プリンタデータがあればそれを表示します
int printerCount = Integer.valueOf(bD.get("プリンタ数"));
if (printerCount < 1){
//
printerSearch();
} else {
TextView[] tvlist = {findViewById(R.id.textView01),
findViewById(R.id.textView02),
findViewById(R.id.textView03)};
int i;
for (i = 0; i < tvlist.length; i++) {
if (i < printerCount ) {
tvlist[i].setText("プリンタ名: " + bD.get("プリンタ名_" + i) + "\n" +
"IPアドレス: " + bD.get("IPアドレス_" + i));
} else {
tvlist[i].setText("");
}
}
}
この件はここまでとします。