Monday, 30 September 2013

Photo storage / sort

Photo storage / sort

I'm wondering what the best practice is for photo storage. I'm in the
business of storing photos with a user. Each user can have up to 35 photos
and we currently have around a 100k users. I currently reference the
photo's from a database.
My proposed solution is a hash of the photo name like so.
f56c0de1c61fdb926e79e8a0a65bd12930c9.jpg
broke out into directories like so
Photo 1
f5/6c/0d/e1c61fdb926e79e8a0a65bd12930c9.jpg
f5/6c/0d/e1c61fdb926e79e8a0a65bd12930c9_thumb.jpg
Photo2
ec/1c/55/bfb660548a6770238668c4b117d92f.jpg
ec/1c/55/bfb660548a6770238668c4b117d92f_thumb.jpg
database Stored in the database as a comma separated array like so for
sort ordering.
ec1c55bfb660548a6770238668c4b117d92f,ec1c55bfb660548a6770238668c4b117d92f
Is this the most efficient way to do this? and is it scalable? I'm not
sure if the photo hash is to large and I'm also not sure if the directory
size is correct.
I created a more detailed post over on stackoverflow, just not sure where
this question belongs.
http://stackoverflow.com/questions/19106596/how-to-store-photo-on-file-system-and-store-sort-order-in-database

Making XMonad redetect two monitors [migrated]

Making XMonad redetect two monitors [migrated]

I accidentally pressed Meta-Q and xmonad restarted. Now, it has mirrored
the same thing on both of my monitors. How do I get it go back to treating
them as two separate screens?
Things I have tried: - Pressing Meta-Q again - Turning one of the monitors
off and back on
This has happened before, and logging out and back in again worked. But I
don't want to do that every time I accidentally press Meta-Q. This has
started happening since I started using dmenu_run to launch apps.
Previously, Meta-Q would restart xmonad harmlessly (without going into
mirrored mode).

Toggling a absolute DIV is not working after firsttime

Toggling a absolute DIV is not working after firsttime

$('.pallete').hide();
$(document).delegate('.pick', 'click', function () {
var pos = $(this).offset();
var x = pos.left - $(window).scrollLeft() + $(this).width();
var y = pos.top - $(window).scrollTop() + $(this).height();
$('.pallete').css({
top: y + "px",
left: x + "px",
}).show();
});
$(document).delegate('.col', 'click', function () {
var pos = $(this).css('background-color');
$('.pick').css('background-color', pos);
$(this).parents('div').fadeOut();
});
Here is the fiddle, http://jsfiddle.net/zPNk3/5/. The problem is when I
click first time on .pick element the '.palette' element is shown
properly. But when I click next time the same is not working.

How to get a TextView at the bottom of the screen under a listview?

How to get a TextView at the bottom of the screen under a listview?

I'm making a settings screen in my android app. This list of course has a
pre defined set of options (notifications/colors/log out/etc.). So I
decided to create the list statically in xml. Because the list should
equally well display if it has more content than the screen I nested my
LinearLayout within a ScrollView (couldn't use a ListView because I want
to statically define the items in it).
This works fine with the following code now:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black" >
<TextView
android:layout_width="fill_parent"
android:layout_height="80dp"
android:paddingTop="30dp"
android:text="@string/account_notifications"
android:background="@color/white"
android:layout_marginBottom="1dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="80dp"
android:text="@string/colors"
android:background="@color/white"
android:layout_marginBottom="1dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="80dp"
android:text="@string/log_out"
android:background="@color/white"
android:layout_marginBottom="1dp" />
</LinearLayout>
</ScrollView>
As you can see I set the background of the LinearLayout to black and the
items to white, with a marginBottom of 1dp, so that there is a separator
between the list items. I now want the logout TextView always at the
bottom of the screen. In case there are more items than the screen can
hold, the Logout should simply be situated at the end of the list.
To get the logout to the bottom of the screen I found some solutions in
this SO question. I first use the first suggestion like so:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black" >
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/list_item_height"
android:text="@string/account_notifications"
android:background="@color/white"
android:layout_marginBottom="1dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/list_item_height"
android:text="@string/colors"
android:background="@color/white"
android:layout_marginBottom="1dp" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/list_item_height"
android:text="@string/log_out"
android:background="@color/white"
android:layout_marginBottom="1dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</ScrollView>
This surprisingly makes the logout TextView totally disappear. So I tried
the second suggestion like so:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black" >
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/list_item_height"
android:layout_weight="1"
android:text="@string/account_notifications"
android:background="@color/white"
android:layout_marginBottom="1dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/list_item_height"
android:layout_weight="1"
android:text="@string/colors"
android:background="@color/white"
android:layout_marginBottom="1dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="@dimen/list_item_height"
android:text="@string/log_out"
android:background="@color/white"
android:layout_marginBottom="1dp" />
</LinearLayout>
</ScrollView>
But this unfortunately doesn't do anything either. No clue why not.
So my question, does anybody know how I can get my logout to the bottom of
the screen, or in case the list is to long for the screen, to the bottom
of the list.
All tips are welcome!

Sunday, 29 September 2013

Store the line break from textarea , store to database and show on frontend?

Store the line break from textarea , store to database and show on frontend?

Currently I am working on storing textarea paragraph
<textarea rows="10"></textarea>
I can save the whole content but the problem is it does not include line
breaks. I wonder what is the best practice of storing the space/line break
from the textarea to database. Also, I need to prevent the user from
inserting the inside the textarea.
Is it good parctice ?
e.g.
text=replace(text,"/n","<br>")
text=replace(text," ","&nbsp;")
text=replace(text,"<script>","")
text=replace(text,"</script>","")
Thanks

Repeating after wrong answer

Repeating after wrong answer

Today I was messing around and I was trying to create a multiple choice
test. I got this far and it works. I was wondering though, how would I
make it repeat the question if the user got the answer wrong? If anyone
could help me out that would be fantastic! Thanks!
import java.util.Scanner;
public class multipleChoiceTest {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("What color is the sky?");
System.out.println("A. Blue");
System.out.println("B. Green");
System.out.println("C. Yellow");
System.out.println("D. Red");
String userChoice = myScanner.nextLine();
if (userChoice.equalsIgnoreCase("a")) {
System.out.println("You're right!");
} else {
System.out.println("You're wrong! Try Again.");
}
}

Boolean value is set to true from the start

Boolean value is set to true from the start

Hey i have been trying to make this work, but i can't figure out what the
problem is.
I want the stopValue to be false when i start the app, and then it is
gonna be set by my actions in the app. I have found out boolean values are
false from the start and therefore my start intent is set to false.
This is not my only problem, when i have entered the start kilometers and
my stop kilometers it give me the stop kilometers only. and the result
should have been start - stop kilometers.
I don't know if these problems have a connection. Maybe i don't get the
sharedpreferences part right or maybe it's the putextra or getIntent part.
Thanks for your time.
My main class
public class Main extends Activity{
Button bStart, bStop;
TextView tvView;
Spinner spinner1;
boolean stopValue;
int startkilometer;
String date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bStart = (Button) findViewById(R.id.bStart);
tvView = (TextView) findViewById(R.id.tvView);
spinner1 = (Spinner) findViewById(R.id.spinner1);
date = getIntent().getStringExtra("datoen");
startkilometer = getIntent().getIntExtra("startkm", 0);
SharedPreferences sp =
getApplicationContext().getSharedPreferences("stopValue", 0);
stopValue = sp.getBoolean("stopper", stopValue);
SharedPreferences sp1 = getSharedPreferences("startkilometer",
Activity.MODE_PRIVATE);
startkilometer = sp1.getInt("startkm", startkilometer);
SharedPreferences sp2 = getSharedPreferences("date",
Activity.MODE_PRIVATE);
date = sp2.getString("datoen", date);
stopValue = getIntent().getBooleanExtra("stopper", stopValue);
if(stopValue){
bStart.setText("Start");
bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent start = new Intent("com.uniqueapps.runner.START");
startActivity(start);
}
});
}
if(stopValue == false){
bStart.setText("Stop");
bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent stop = new Intent("com.uniqueapps.runner.STOP");
stop.putExtra("startkm", startkilometer);
stop.putExtra("datoen", date);
startActivity(stop);
}
});
}
KilometerSQL info = new KilometerSQL(this);
info.open();
String data = info.getData();
info.close();
tvView.setText(data);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
SharedPreferences sp =
getApplicationContext().getSharedPreferences("stopValue", 0);
Editor edit = sp.edit();
edit.putBoolean("stopper", stopValue);
edit.commit();
SharedPreferences sp1 =
getApplicationContext().getSharedPreferences("startkilometer", 0);
Editor edit1 = sp1.edit();
edit1.putInt("startkm", startkilometer);
edit1.commit();
SharedPreferences sp11 =
getApplicationContext().getSharedPreferences("date", 0);
Editor edit11 = sp11.edit();
edit11.putString("datoen", date);
edit11.commit();
super.onPause();
}
My start class
public class Start extends Main implements OnClickListener {
Button bStartTur;
EditText etDate, etKm;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
bStartTur = (Button) findViewById(R.id.bStartTur);
bStartTur.setOnClickListener(this);
etDate = (EditText) findViewById(R.id.etdate);
etKm = (EditText) findViewById(R.id.etKm);
Calendar cal = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
etDate.setText(format.format(new Date()));
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String date = etDate.getText().toString();
int startkilometer;
switch (v.getId()) {
case R.id.bStartTur:
startkilometer = Integer.valueOf(etKm.getText().toString());
Intent menu = new Intent("com.uniqueapps.runner.MENU");
menu.putExtra("stopper", false);
menu.putExtra("startkm", startkilometer);
menu.putExtra("datoen", date);
startActivity(menu);
break;
}
}
}
My stop class
public class Stop extends Main implements OnClickListener {
Button bStop;
EditText sqllocations, kilometer;
int startkilometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.stop);
bStop = (Button) findViewById(R.id.bstopTur);
bStop.setOnClickListener(this);
sqllocations = (EditText) findViewById(R.id.locations);
kilometer = (EditText) findViewById(R.id.kilometer);
sqllocations.setText("Unknown");
}
@Override
public void onClick(View v) {
int startkilometer;
int slutkilometer;
startkilometer = getIntent().getIntExtra("startkm", 0);
date = getIntent().getStringExtra("datoen");
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bstopTur:
slutkilometer = Integer.valueOf(kilometer.getText().toString());
int kortekm = (slutkilometer - startkilometer);
try{
String locations = sqllocations.getText().toString();
KilometerSQL entry = new KilometerSQL(this);
entry.open();
entry.createEntry(date, kortekm, locations);
entry.close();
}catch(Exception e){
e.printStackTrace();
}
Intent menu = new Intent("com.uniqueapps.runner.MENU");
menu.putExtra("stopper", true);
startActivity(menu);
break;
}
}
}
My SQL class
public class KilometerSQL {
public static final String KEY_ROWID = "date";
public static final String KEY_KILOMETER = "kilometer";
public static final String KEY_LOCATIONS = "locations";
private static final String DATABASE_NAME = "Kilometerdb";
private static final String DATABASE_TABLE = "kilometertable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " TEXT NOT NULL, " +
KEY_KILOMETER + " INTEGER, " +
KEY_LOCATIONS + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public KilometerSQL (Context c){
ourContext = c;
}
public KilometerSQL open(){
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String date, int kortekm, String locations) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_ROWID, date);
cv.put(KEY_KILOMETER, kortekm);
cv.put(KEY_LOCATIONS, locations);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String [] columns = new String []{ KEY_ROWID, KEY_KILOMETER,
KEY_LOCATIONS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null,
null, null, null);
String result = "";
int iDate = c.getColumnIndex(KEY_ROWID);
int iKilometer = c.getColumnIndex(KEY_KILOMETER);
int iLocations = c.getColumnIndex(KEY_LOCATIONS);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
result = result + c.getString(iDate) + " " +
c.getString(iKilometer) + " " + c.getString(iLocations) + "\n";
}
return result;
}
}