Tuesday, April 24, 2012

Wicket AbstractAjaxTimerBehavior and performance

I'm using AbstractAjaxWicketBehavior in my Wicket application and it seems to have a descending performance over time when more AJAX calls appear. When a page is refreshed without AJAX, the performance is fine again. I would like to know if this is a normal thing or can be a memory leak of some kind present? I can't simply attach the code as it's spread over more classes and it would require too much effort to understand, but in short I want to do this:




  1. create and start the timer

  2. repeat some code 10x

  3. stop the timer

  4. set some values to attributes

  5. ajax refresh (causes show/hide of some components)



and do the same again (hypotetically infinite times).



Every repetition of this flow is slower even though I use constant updating interval of 100ms.



As the timer is a behavior and does not allow to be restarted or reused, it is created every time as a new instance and attached to the Form component.



The timer looks like this:



static int count = 0
new AbstractAjaxTimerBehavior(Duration.milliseconds(100)) {
// do some code
count++
if(count == 10) {
stop();
// do some code
}
}


This behavior is attached to a Form inside a Panel and upon a click on an AjaxLink the Form is refreshed (added to AjaxRequestTarget). Every time I remove the old timer from the Form component before adding the new behavior.



Everything works fine, but every repetition of this procedure runs slower (The first one is perfect, the second one is also around 100ms, but then it gets slower (after 10 or 15 repetitions, the refresh interval is about 1 second) and all other AJAX calls in the app also go significantly slower), so I suspect there is a memory leak... any obvious reasons? Or any ways how to make a wicket timer better for my purpose? Any advice appreciated. Thanks.





jQuery: $(element).text() doesn't work

Why isn't this working? I've searched Stackoverflow and jQuery API and it should work. Instead I get an empty string :(



function a(){
var div=document.createElement("div");
div.innerHTML='<a class="aClass" href="javascript:showMyText(this)">Link Text</a>';

var parent_div=document.getElementById('dinamicni_div');
parent_div.appendChild(div);
}

function showMyText(link){
var txt=$(link).text();
alert(txt);
}




Transition between 2 movies using a MPMoviePlayerController

I'm trying to make a smooth cross fade between 2 movies in my iPad App. On a button tap the visuals randomize & the movie which is playing fades to the next movie.



Since 2 movies cannot be played simultaneously (from the docs) a straight-forward cross-fade isn't possible. To get around this I use a still image of the initial frame of the new movie (which is to be faded in). I used two players to handle the interchange of movies.



The original movie is paused, Then I transition to the image. Now I add my 2nd movie (with alpha = 0.0) & then transition so that its alpha = 1. I then play the new movie. This all works ok except for a noticeable darkening as the new movie transitions in over the image.



I tested it without the still image & the darkening doesn't seem to occur during said transition.



My code for the switch is below (first half of the if-statement). It looks pretty strange ,looking over it, but it's the method that brought me closest to what I needed.



Maybe there's a better way to go about this task? Thanks in advance :)



- (void) switchMovie;
{

NSURL *movieImageUrl = [movieImageUrlArray objectAtIndex:index];
NSData *data = [NSData dataWithContentsOfURL:movieImageUrl];
UIImage *movieImage = [[UIImage alloc] initWithData:data];
UIImageView *image = [[UIImageView alloc] initWithImage:movieImage];

image.alpha = 0.0f;
[self addSubview:image];

if (moviePlayer1Playing) {

moviePlayer1Playing = NO;
moviePlayer2Playing = NO;

[moviePlayer1 pause]; //pausing later in the method caused a crash, stopping causes a crash.

[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationCurveEaseIn
animations:^{

image.alpha = 1.0f; //fade in still of first frame of new movie

} completion:^(BOOL finished) {

[moviePlayer1 release]; //release original movie.

moviePlayer2 = [[MPMoviePlayerController alloc] initWithContentURL:[movieUrlArray objectAtIndex:index]];

moviePlayer2.shouldAutoplay = NO;
[moviePlayer2 prepareToPlay];
[moviePlayer2 setControlStyle:MPMovieControlStyleNone];
moviePlayer2.scalingMode = MPMovieScalingModeAspectFill;
[moviePlayer2.view setFrame:CGRectMake(0, 0, 1024 , 768)];
moviePlayer2.view.backgroundColor = [UIColor clearColor];
moviePlayer2.view.alpha = 0.0;
[self addSubview:moviePlayer2.view];

[UIView animateWithDuration: 3.0
delay: 0.0
options: UIViewAnimationCurveLinear
animations:^(void){

moviePlayer2.view.alpha = 1.0f;
//During this transition the view goes dark half-way through, before lightening again.

} completion:^ (BOOL finished) {

[moviePlayer2 play];
moviePlayer2Playing = YES;
moviePlayer1Playing = NO;


}];

}];


}




Detecting button clicks within a for loop in an Android app

I'm having a little bit of trouble. There are 12 buttons, and 10 questions in my android app. If I ran a for loop for all of the questions is there a way that I can check if a button is pushed and if it's the answer to the current question? Then it would move on to the next question, and do the same until the loop ended. I don't know exactly how to word it, but I hope you can figure out what I mean.





Disabling button from another class

I know this question has been asked before or similar questions asked but none of the solutions has yet fixed my problem.



I have a winform that has a button named hitButton. All I need to do is to disable this button from another class (i.e. not the form class).



My form class is:



public partial class BlackJackTable : Form
{
}


My separate class is:



class GameWon
{
}


I have tried exposing a public method in BlackJackTable like this:



public void DisableButtons()
{
hitButton.Enabled = false;
}


and accessing from GameWon constructor (it doesn't seem to matter where in the class I access it from though) like this:



public BlackJackTable blackJackTable = new BlackJackTable();

public GameWon()
{
blackJackTable.DisableButtons();
}


I have tried using a variable in GameWon:



public Button hit;


and assigning from blackJackTable:



GameWon gameWon = new GameWon();

public void Assign()
{
gameWon.hit = this.hitButton; // or just = hitButton;
}


and disabling from GameWon:



hit.Enabled = false;


I have tried a couple of other things but none of them work. I'm guessing I'm missing something obvious as none of the methods I've tried have worked.



Any thoughts appreciated!



Update



I have managed to do the latter method with textboxes and pictureboxes, what's the difference with a button?





Populate a ListView with an AsyncTask failing to populate

Trying to get this working... it loads up fine, even tells the application that it completed getting all the data. It does not populate the listview though.



The data response inside show_list: [A, B, C, D]



public class MainActivity extends ActionBarActivity {
private static final String DEBUG_TAG = "MainActivity";
private boolean mAlternateTitle = false;
List<Show> mShowList;
AlertDialog mAlertDialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadTitles();
}

private void loadTitles() {
ShowsList show_list = new ShowsList();
show_list.execute();
}

private class ShowsList extends AsyncTask<Void, Void, List<Show>> {
@Override
protected void onPreExecute() {
mAlertDialog = new AlertDialog.Builder(MainActivity.this).setIcon(R.drawable.ic_action_refresh).setTitle(R.string.fetching_new).show();
}

@Override
protected List<Show> doInBackground(Void... voids) {
try {
mShowList = Show.getShows();
return mShowList;
} catch (Exception e) {
new AlertDialog.Builder(MainActivity.this.getApplicationContext()).setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.server_down_title).setMessage(R.string.server_down_message).setPositiveButton(R.string.app_quit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
MainActivity.this.finish();
}
}).show();
return null;
}
}

@Override
protected void onPostExecute(final List<Show> show_list) {
final String DEBUG_TAG = "MainActivity$ShowList$onPostExecute";
/*ListView lv = (ListView) findViewById(R.id.list);
ArrayAdapter<Show> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, async_show_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new ListClickListener());
*/
if (mAlertDialog.isShowing()) {
mAlertDialog.dismiss();
}

try {
ListView lv = (ListView) findViewById(R.id.list);
ShowsAdapter adapter = new ShowsAdapter(MainActivity.this, android.R.layout.simple_list_item_1, show_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new ListClickListener());
Log.d(DEBUG_TAG, adapter.toString());
adapter.notifyDataSetChanged();
//adapter.add(show_list[0]);
} catch (Exception e) {
new AlertDialog.Builder(MainActivity.this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.server_down_title).setMessage(R.string.server_down_message).setPositiveButton(R.string.app_quit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
MainActivity.this.finish();
}
}).show();
}
}
}

private class ListClickListener implements AdapterView.OnItemClickListener {
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Show show = mShowList.get(i);
Toast.makeText(MainActivity.this, "Clicked on a list item: " + show.title, Toast.LENGTH_LONG).show();
}
}

private class ShowsAdapter extends ArrayAdapter<Show> {
public ShowsAdapter(Context context, int textViewResourceId, List<Show> shows) {
super(context, textViewResourceId, shows);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Show show = this.getItem(position);

if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_row_show, parent, false);
}

((TextView) convertView.findViewById(R.id.show_title)).setText(show.title);
//((TextView) convertView.findViewById(R.id.episode_number)).setText(episode.getGrayLine());

return convertView;
}
}




Random image in a .php file

Im trying to make a random image display with php. Now im not talking about something that echos "image", I need something that when I view the .png file I will see a random image everytime. I wasnt sure what to search for so Im gonna ask it here. I saw someone do it before, where they 'tricked' the browser into thinking it was a .png file, when it was just some php using something to pick a random image and set it as the file.