Repository navigation
Expand file tree
/
Copy pathVideoDepthProgressRow.java
More file actions
65 lines (65 loc) · 3.54 KB
/
Copy pathVideoDepthProgressRow.java
File metadata and controls
65 lines (65 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package dev.denver.dressroomadvanced;
import android.content.Context;
import android.os.*;
import android.view.View;
import android.widget.*;
import java.util.concurrent.*;
/** Read actual broker counters off the UI thread; never query while hidden. */
final class VideoDepthProgressRow implements View.OnAttachStateChangeListener,Runnable {
private static final ExecutorService IO=Executors.newSingleThreadExecutor();
private final TextView label;
private final ProgressBar bar;
private int generation;
private boolean pending;
private VideoDepthProgressRow(TextView text) {
label=text;
bar=new ProgressBar(text.getContext(),null,android.R.attr.progressBarStyleHorizontal);
bar.setMax(100);bar.setVisibility(View.GONE);
if(text.getParent() instanceof LinearLayout parent) {
int index=parent.indexOfChild(text);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-2);
if(text.getLayoutParams() instanceof LinearLayout.LayoutParams old){
lp.setMarginStart(old.getMarginStart());lp.setMarginEnd(old.getMarginEnd());
}
parent.addView(bar,index+1,lp);
}
label.addOnAttachStateChangeListener(this);
if(label.isAttachedToWindow())onViewAttachedToWindow(label);
}
static void attach(TextView text){new VideoDepthProgressRow(text);}
@Override public void onViewAttachedToWindow(View view){generation++;label.removeCallbacks(this);label.post(this);}
@Override public void onViewDetachedFromWindow(View view){generation++;label.removeCallbacks(this);pending=false;}
@Override public void run(){
if(!label.isAttachedToWindow())return;
if(!label.isShown()||label.getWindowVisibility()!=View.VISIBLE){label.postDelayed(this,2000);return;}
if(pending)return;
pending=true;int token=generation;Context c=label.getContext();
IO.execute(()->{
Bundle b=null;
try{b=c.getContentResolver().call(VideoDepthBroker.URI,"progress",null,null);}
catch(RuntimeException|LinkageError ignored){}
final Bundle result=b;
label.post(()->{
if(token!=generation||!label.isAttachedToWindow())return;
pending=false;
String status=result==null?"unavailable":result.getString("status","idle");
int done=result==null?0:result.getInt("completed"),total=result==null?0:result.getInt("total");
boolean generating="generating".equals(status);
bar.setVisibility(generating?View.VISIBLE:View.GONE);
bar.setIndeterminate(generating&&total<=0);
if(generating&&total>0)bar.setProgress(Math.min(100,done*100/total),true);
String message=switch(status){
case "generating" -> total>0?"Analyzing last applied video: "+done+" / "+total:"Preparing last applied video…";
case "ready" -> "Last applied video: depth mask ready";
case "failed" -> "Last applied video: detection failed. Tap Retry.";
case "disabled" -> "Video depth disabled";
case "manual" -> "Manual mask mode";
case "unavailable" -> "Video depth service unavailable";
default -> "Apply the video and wake the lock screen to start detection.";
};
if(!message.contentEquals(label.getText()))label.setText(message);
label.postDelayed(this,generating?1000:2500);
});
});
}
}