Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shinovon committed Nov 21, 2021
1 parent 52117b9 commit c66f63a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 57 deletions.
36 changes: 20 additions & 16 deletions src/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,32 @@ public class App implements CommandListener, Constants {
private Object addLock = new Object();

private Vector queuedTasks = new Vector();
protected Object tasksLock = new Object();
private Object tasksLock = new Object();
private Thread tasksThread = new Thread() {
public void run() {
while(midlet.running) {
try {
synchronized (tasksLock) {
tasksLock.wait();
}
while(queuedTasks.size() > 0) {
try {
((Runnable)queuedTasks.elementAt(0)).run();
} catch (Exception e) {
e.printStackTrace();
}
queuedTasks.removeElementAt(0);
Thread.yield();
}
} catch (InterruptedException e) {
return;
}
while(queuedTasks.size() > 0) {
try {
((Runnable)queuedTasks.elementAt(0)).run();
} catch (Exception e) {
e.printStackTrace();
}
queuedTasks.removeElementAt(0);
Thread.yield();
}
}
}
};

public void scheduleRunnable(Runnable r) {
if(queuedTasks.contains(r)) return;
queuedTasks.addElement(r);
synchronized(tasksLock) {
tasksLock.notify();
Expand Down Expand Up @@ -226,14 +227,14 @@ private void loadTrends() {
mainForm.setTitle(NAME + " - " + Locale.s(TITLE_Trends));
JSONArray j = (JSONArray) invApi("v1/trending?fields=" + VIDEO_FIELDS + (videoPreviews ? ",videoThumbnails" : ""));
try {
if(mainForm.get(0) == loadingItem) {
if(mainForm.size() > 0 && mainForm.get(0) == loadingItem) {
mainForm.delete(0);
}
} catch (Exception e) {
}
int l = j.size();
for(int i = 0; i < l; i++) {
Item item = parseAndMakeItem(j.getObject(i), false);
Item item = parseAndMakeItem(j.getObject(i), false, i);
if(item == null) continue;
mainForm.append(item);
if(i >= TRENDS_LIMIT) break;
Expand Down Expand Up @@ -268,14 +269,14 @@ private void loadPopular() {
mainForm.setTitle(NAME + " - " + Locale.s(TITLE_Popular));
JSONArray j = (JSONArray) invApi("v1/popular?fields=" + VIDEO_FIELDS + (videoPreviews ? ",videoThumbnails" : ""));
try {
if(mainForm.get(0) == loadingItem) {
if(mainForm.size() > 0 && mainForm.get(0) == loadingItem) {
mainForm.delete(0);
}
} catch (Exception e) {
}
int l = j.size();
for(int i = 0; i < l; i++) {
Item item = parseAndMakeItem(j.getObject(i), false);
Item item = parseAndMakeItem(j.getObject(i), false, i);
if(item == null) continue;
mainForm.append(item);
if(i >= TRENDS_LIMIT) break;
Expand Down Expand Up @@ -313,8 +314,9 @@ private void search(String q) {
JSONArray j = (JSONArray) invApi("v1/search?q=" + Util.url(q) + "&fields=" + SEARCH_FIELDS + ",type" + (videoPreviews ? ",videoThumbnails" : "") + (searchChannels ? "&type=all" : ""));
int l = j.size();
for(int i = 0; i < l; i++) {
Item item = parseAndMakeItem(j.getObject(i), true);
Item item = parseAndMakeItem(j.getObject(i), true, i);
if(item == null) continue;

searchForm.append(item);
if(i >= SEARCH_LIMIT) break;
if(b) checkMemoryAndGc();
Expand All @@ -328,17 +330,19 @@ private void search(String q) {
gc();
}

private Item parseAndMakeItem(JSONObject j, boolean search) {
private Item parseAndMakeItem(JSONObject j, boolean search, int i) {
String type = j.getNullableString("type");
if(type == null) {
// video
VideoModel v = new VideoModel(j);
v.setIndex(i);
if(search) v.setFromSearch();
if(videoPreviews) addAsyncLoad(v);
return v.makeItemForList();
}
if(type.equals("video")) {
VideoModel v = new VideoModel(j);
v.setIndex(i);
if(search) v.setFromSearch();
if(videoPreviews) addAsyncLoad(v);
return v.makeItemForList();
Expand Down
4 changes: 2 additions & 2 deletions src/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ private void fail(String s, String title) {
}

private void info(String s, int percent) {
if(percent >= 0) {
s += " " + s + "%";
if(percent >= 0 && percent <= 100) {
s += " " + percent + "%";
indicator.setValue(percent);
}
info(s);
Expand Down
2 changes: 1 addition & 1 deletion src/Records.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static Image saveOrGetImage(String id, String url) throws IOException {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore(id, true);
System.out.println(id + " " + rs + " " + rs.getNumRecords());
//System.out.println(id + " " + rs + " " + rs.getNumRecords());
if(rs.getNumRecords() > 0) {
b = rs.getRecord(1);
}
Expand Down
17 changes: 12 additions & 5 deletions src/models/VideoModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public class VideoModel extends AbstractModel implements ItemCommandListener, IL
private VideoItem customItem;

private Form formContainer;
private int index;

private int index = -1;

// create model without parsing
public VideoModel(String id) {
Expand Down Expand Up @@ -132,6 +133,12 @@ public ImageItem makeImageItemForPage() {
if(customItem != null) {
img = customItem.getImage();
}
if(img == null && App.rmsPreviews) {
try {
img = Records.saveOrGetImage(videoId, thumbnailUrl);
} catch (IOException e) {
}
}
return imageItem = new ImageItem(null, img, Item.LAYOUT_CENTER, null);
}

Expand All @@ -154,11 +161,11 @@ public Image customResize(Image img) {
public void loadImage() {
if(img != null) return;
if(thumbnailUrl == null) return;
if(imageItem == null && customItem == null) return;
if(imageItem == null && customItem == null && !extended) return;
try {
if(App.rmsPreviews) {
if(index <= 1) {
if(!App.customItems) {
if(index <= 1 && index != -1) {
if(imageItem != null) {
float iw = img.getWidth();
float ih = img.getHeight();
float nw = (float) imageWidth;
Expand All @@ -175,7 +182,7 @@ public void loadImage() {
} else {
byte[] b = App.hproxy(thumbnailUrl);
img = Image.createImage(b, 0, b.length);
if(!App.customItems) {
if(imageItem != null) {
float iw = img.getWidth();
float ih = img.getHeight();
float nw = (float) imageWidth;
Expand Down
7 changes: 6 additions & 1 deletion src/ui/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ public void show() {


public static void loadConfig() {
System.out.println("rmsPreviews " + App.rmsPreviews);
/*
String s = System.getProperty("kemulator.libvlc.supported");
if(s != null && s.equals("true")) {
App.watchMethod = 1;
}
*/
RecordStore r = null;
try {
r = RecordStore.openRecordStore(CONFIG_RECORD_NAME, false);
Expand Down
83 changes: 51 additions & 32 deletions src/ui/custom/VideoItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ public class VideoItem extends CustomButtonItem {
private Runnable loadImgRMS = new Runnable() {
public void run() {
try {
if(!drawn) return;
Image img = Records.saveOrGetImage(video.getVideoId(), null);
if(img == null) {
System.out.println("img null " + VideoItem.this.toString());
//System.out.println("img null " + VideoItem.this.toString());
return;
}
System.out.println("img " + VideoItem.this.toString());
//System.out.println("img " + VideoItem.this.toString());
setImage(video.customResize(img));
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -49,13 +50,16 @@ public void run() {
private Runnable setImgNull = new Runnable() {
public void run() {
try {
if(!drawn) return;
setImage(null);
} catch (Exception e) {
e.printStackTrace();
}
}
};

private boolean drawn;

public VideoItem(VideoModel v) {
super(v);
this.video = v;
Expand All @@ -67,6 +71,7 @@ public VideoItem(VideoModel v) {
}

protected void paint(Graphics g, int w, int h) {
drawn = true;
width = w;
height = h;
int ih = getImgHeight();
Expand Down Expand Up @@ -107,7 +112,11 @@ private void makeTitleArr() {
if(arr.length > 0) {
titleArr[0] = arr[0];
if(arr.length > 1) {
titleArr[1] = arr[1];
if(arr.length > 2) {
titleArr[1] = arr[1].concat("...");
} else {
titleArr[1] = arr[1];
}
}
}
title = null;
Expand All @@ -125,15 +134,15 @@ private int getImgHeight() {

private int getTextMaxWidth() {
if(textWidth > 0) return textWidth;
int w = width - 4;
int w = width - 6;
if(w <= 0) {
w = App.width - 8;
w = App.width - 10;
}
int i;
if(lengthStr != null) {
i = smallfont.stringWidth(lengthStr) + 8;
i = smallfont.stringWidth(lengthStr) + 10;
} else {
i = smallfont.stringWidth(" AA:AA:AA") + 8;
i = smallfont.stringWidth(" AA:AA:AA") + 10;
}
return textWidth = w - i;
}
Expand Down Expand Up @@ -190,14 +199,14 @@ private static String timeStr(int i) {
}

protected void showNotify() {
System.out.println("showNotify " + toString());
//System.out.println("showNotify " + toString());
if(App.rmsPreviews) {
App.inst.scheduleRunnable(loadImgRMS);
}
}

protected void hideNotify() {
System.out.println("hideNotify " + toString());
//System.out.println("hideNotify " + toString());
if(App.rmsPreviews) {
App.inst.scheduleRunnable(setImgNull);
}
Expand All @@ -212,34 +221,44 @@ private static String[] getStringArray(String text, int maxWidth, Font font) {
return new String[0];
}
Vector v = new Vector(3);
v.addElement(text);
for (int i = 0; i < v.size(); i++) {
String s = (String) v.elementAt(i);
if(font.stringWidth(s) >= maxWidth) {
final int max = 3;
v: {
if (font.stringWidth(text) > maxWidth) {
int i1 = 0;
for (int i2 = 0; i2 < s.length(); i2++) {
if (font.stringWidth(s.substring(i1, i2)) >= maxWidth) {
boolean space = false;
for (int j = i2; j > i1; j--) {
char c = s.charAt(j);
if (c == ' ' || (c >= ',' && c <= '/')) {
space = true;
v.setElementAt(s.substring(i1, j + 1), i);
v.insertElementAt(s.substring(j + 1), i + 1);
i += 1;
i2 = i1 = j + 1;
break;
for (int i2 = 0; i2 < text.length(); i2++) {
if(v.size() >= max) break v;
if (text.charAt(i2) == '\n') {
v.addElement(text.substring(i1, i2));
i2 = i1 = i2 + 1;
} else {
if (text.length() - i2 <= 1) {
v.addElement(text.substring(i1, text.length()));
break;
} else if (font.stringWidth(text.substring(i1, i2)) >= maxWidth) {
boolean space = false;
for (int j = i2; j > i1; j--) {
char c = text.charAt(j);
if (c == ' ' || (c >= ',' && c <= '/')) {
String s = text.substring(i1, j + 1);
if(font.stringWidth(s) >= maxWidth - 1) {
continue;
}
space = true;
v.addElement(s);
i2 = i1 = j + 1;
break;
}
}
if (!space) {
i2 = i2 - 2;
v.addElement(text.substring(i1, i2));
i2 = i1 = i2 + 1;
}
}
if (!space) {
i2 = i2 - 2;
v.setElementAt(s.substring(i1, i2), i);
v.insertElementAt(s.substring(i2), i +1);
i2 = i1 = i2 + 1;
i += 1;
}
}
}
} else {
return new String[] { text };
}
}
String[] arr = new String[v.size()];
Expand Down

0 comments on commit c66f63a

Please sign in to comment.