Wednesday, November 27, 2013

Friend classes

In principle, private and protected members of a class cannot be accessed from outside the same class in which they are declared. However, this rule does not affect friends.

Friends are functions or classes declared with the friend keyword.
A class as friend of another one, granting that first class access to the protected and private members of the second one.

/ friend class
#include <iostream>

class CSquare;

class CRectangle {
    int width, height;
  public:
    int area ()
      {return (width * height);}
    void convert (CSquare a);
};

class CSquare {
  private:
    int side;
  public:
    void set_side (int a)
      {side=a;}
    friend class CRectangle;
};

void CRectangle::convert (CSquare a) {
  width = a.side;
  height = a.side;
}
  
int main () {
  CSquare sqr;
  CRectangle rect;
  sqr.set_side(4);
  rect.convert(sqr);
  cout << rect.area();
  return 0;
}

Friend functions

Private and protected members of a class cannot be accessed from outside the same class in which they are declared. However, this rule does not affect friends,

A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

Friends are functions or classes declared with the friend keyword.
                          If we want to declare an external function as friend of a class, thus allowing this function to have access to the private and protected members of this class, we do it by declaring a prototype of this external function within the class, and preceding it with the keyword friend: 

#include <iostream>
 
 
class Student
{
   double id;
public:
   friend void printid( Student Student );
   void setId( double wid );
};

// Member function definition
void  Student ::setId( double wid )
{
    id = wid;
}

// Note: printId() is not a member function of any class.
void printId(  Student  student )
{
   /* Because setid() is a friend of Student, it can
    directly access any member of this class */
   cout << "Id of student : " << student .id <<endl;
}
 
// Main function for the program
int main( )
{
    Student  student ;
 
   // set student id without member function
   student .setId(10.0);
   
   // Use friend function to print the wdith.
   printId( student );
 
   return 0;
}

What is Arduino?

                 

Arduino is a tool for making computers that can sense and control more of the physical world than your desktop computer. It's an open-source physical computing platform based on a simple microcontroller board, and a development environment for writing software for the board.

   Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It’s intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.

Arduino can be used to develop interactive objects, taking inputs from a variety of switches or sensors, and controlling a variety of lights, motors, and other physical outputs. Arduino projects can be stand-alone, or they can be communicate with software running on your computer (e.g. Flash, Processing, MaxMSP.) The boards can be assembled by hand or purchased preassembled; the open-source IDE can be downloaded for free.

Thursday, February 7, 2013

Dynamic gradient in android




Step One:- create a class
import android.graphics.drawable.GradientDrawable;

public class DrawableGradient extends GradientDrawable {
public DrawableGradient(int[] colors, int cornerRadius) {
super(GradientDrawable.Orientation.BR_TL, colors);

try {
this.setShape(GradientDrawable.RECTANGLE);
this.setGradientType(GradientDrawable.RECTANGLE);
this.setCornerRadius(cornerRadius);
} catch (Exception e) {
e.printStackTrace();
}
}

public DrawableGradient SetTransparency(int transparencyPercent) {
this.setAlpha(255 - ((255 * transparencyPercent) / 100));

return this;
}
}

step 2 : use this
TextView.setBackgroundDrawable(new DrawableGradient(new int[] {
0xFFFF0000, 0xFF800000, 0xFF000000 }, 0).SetTransparency(10));

email validation in android




protected final Pattern EMAIL_ADDRESS_PATTERN = Pattern
.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@"
+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\."
+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+");

/**
* for email validation
*
* @param str
*            = string for validation
* @return true if validation is correct
*/
public boolean emailValidation(String str) {
if (str.equals("")
|| EMAIL_ADDRESS_PATTERN.matcher(str).matches() == false)
return false;
else
return true;
}

Android Custom Calendar


                                           Android Custom Calendar   

import java.text.DateFormatSymbols;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * @author Sudhi.S
 *
 */
public class UDSCalender extends Dialog implements
android.view.View.OnClickListener {

private TextView txt1 = null;
private TextView txt = null;
private GridView gd = null;
private Calendar calendar = null;

public UDSCalender(Context context) {

super(context, android.R.style.Theme_Panel);

calendar = Calendar.getInstance();
LinearLayout mreal = new LinearLayout(getContext());
LinearLayout.LayoutParams mrealp = new LinearLayout.LayoutParams(-1, -2);
mreal.setBackgroundResource(R.drawable.abs__dialog_full_holo_light);
mreal.setOrientation(LinearLayout.VERTICAL);
View vo = new View(getContext());
vo.setBackgroundColor(Color.parseColor("#ffffbb33"));
vo.setLayoutParams(new LinearLayout.LayoutParams(-1, 2));
mreal.addView(vo);
TextView txt = new TextView(getContext());
txt.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
txt.setText("Calendar");
txt.setPadding(0, 5, 0, 5);

txt.setTextColor(Color.BLACK);
txt.setGravity(Gravity.CENTER);
mreal.addView(txt);
View vo1 = new View(getContext());
vo1.setBackgroundColor(Color.parseColor("#ffffbb33"));
vo1.setLayoutParams(new LinearLayout.LayoutParams(-1, 2));
mreal.addView(vo1);
addButton(mreal);
View vo3 = new View(getContext());
vo3.setBackgroundColor(Color.parseColor("#ffffbb33"));
vo3.setLayoutParams(new LinearLayout.LayoutParams(-1, 2));
mreal.addView(vo3);
addCalender(mreal);
View vo2 = new View(getContext());
vo2.setBackgroundColor(Color.parseColor("#ffffbb33"));
vo2.setLayoutParams(new LinearLayout.LayoutParams(-1, 2));
mreal.addView(vo2);
this.setCancelable(true);
this.addContentView(mreal, mrealp);

}
ArrayList<Date> dater=null;

public void setArrayList(ArrayList<Date> dater)
{
this. dater=dater;
}
public String getMonth(int month) {
return new DateFormatSymbols().getMonths()[month];
}

private void addButton(LinearLayout mreal) {

int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
LinearLayout ll = new LinearLayout(getContext());
ll.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
ll.setOrientation(LinearLayout.HORIZONTAL);
ImageView img = new ImageView(getContext());
img.setImageResource(android.R.drawable.btn_minus);
img.setLayoutParams(new LinearLayout.LayoutParams(-1, -2, 1.2f));
img.setOnClickListener(this);
img.setTag("0");
ll.addView(img);
txt = new TextView(getContext());
txt.setLayoutParams(new LinearLayout.LayoutParams(-1, -2, 1));

txt.setText(year + "");

txt.setTextColor(Color.BLACK);
txt.setGravity(Gravity.CENTER);
ll.addView(txt);
ImageView img1 = new ImageView(getContext());
img1.setImageResource(android.R.drawable.btn_plus);
img1.setLayoutParams(new LinearLayout.LayoutParams(-1, -2, 1.2f));
img1.setTag("1");
img1.setOnClickListener(this);
ll.addView(img1);

ImageView img3 = new ImageView(getContext());
img3.setImageResource(android.R.drawable.btn_minus);
img3.setLayoutParams(new LinearLayout.LayoutParams(-1, -2, 1.2f));
img3.setTag("2");
img3.setOnClickListener(this);
ll.addView(img3);
txt1 = new TextView(getContext());
txt1.setLayoutParams(new LinearLayout.LayoutParams(-1, -2, 1));
txt1.setText(getMonth(month));
txt1.setTag(month);
txt1.setTextColor(Color.BLACK);
txt1.setGravity(Gravity.CENTER);
ll.addView(txt1);
ImageView img4 = new ImageView(getContext());
img4.setImageResource(android.R.drawable.btn_plus);
img4.setLayoutParams(new LinearLayout.LayoutParams(-1, -2, 1.2f));
img4.setTag("3");
img4.setOnClickListener(this);
ll.addView(img4);
mreal.addView(ll);

}

private void addCalender(LinearLayout mreal) {
gd = new GridView(getContext());
gd.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));

gd.setNumColumns(7);
gd.setCacheColorHint(android.R.color.transparent);
ArrayList<String> dat = getarrayList((calendar.get(Calendar.MONTH)),
calendar.get(Calendar.YEAR));

newmyadapter ad = setadap(dat);
gd.setAdapter(ad);
mreal.addView(gd);

}

private newmyadapter setadap(ArrayList<String> dat) {
newmyadapter ad = new newmyadapter( dat);
return ad;
}

private ArrayList<String> getarrayList(int month, int year) {
ArrayList<String> dat = new ArrayList<String>();
dat.add("Sun");
dat.add("Mon");
dat.add("Tue");
dat.add("Wed");
dat.add("Thu");
dat.add("Fri");
dat.add("Sat");


Calendar mycal =Calendar.getInstance();
mycal.set(year, month, 1);
System.out.println(year+"-"+month);
int days= mycal.getActualMaximum(Calendar.DAY_OF_MONTH);

try {
Date date1 = (new GregorianCalendar(year, month, 1)).getTime();

Format formatter = new SimpleDateFormat("EEEE");
String s = formatter.format(date1);

int h = 0;
String ne = s.toString().substring(0, 3);
System.out.println("ne" + ne);
if (ne.equalsIgnoreCase("sun")) {
h = 0;
} else if (ne.equalsIgnoreCase("mon")) {
h = 1;
} else if (ne.equalsIgnoreCase("tue")) {
h = 2;

} else if (ne.equalsIgnoreCase("wed")) {
h = 3;
} else if (ne.equalsIgnoreCase("thu")) {
h = 4;
} else if (ne.equalsIgnoreCase("fri")) {
h = 5;

} else if (ne.equalsIgnoreCase("sat")) {
h = 6;

}
System.out.println(h + "h");
for (int j = 0; j < h; j++) {
dat.add(".");
System.out.println(j + "h...");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println(days);
for (int i = 1; i < days + 1; i++) {
dat.add(i + "");
}
return dat;
}

@Override
public void onClick(View v) {
int tag = Integer.parseInt(v.getTag().toString());
int next;
switch (tag) {
case 0:
next = Integer.parseInt(txt.getText().toString().trim()) - 1;
txt.setText(next + "");
gd.setAdapter(setadap(getarrayList(
Integer.parseInt(txt1.getTag().toString().trim()), next)));
break;
case 1:
next = Integer.parseInt(txt.getText().toString().trim()) + 1;
txt.setText(next + "");
gd.setAdapter(setadap(getarrayList(
Integer.parseInt(txt1.getTag().toString().trim()), next)));
break;
case 2:
next = (Integer.parseInt(txt1.getTag().toString().trim()) == 1) ? Integer
.parseInt(txt1.getTag().toString().trim()) : Integer
.parseInt(txt1.getTag().toString().trim()) - 1;

txt1.setText(getMonth(next));
txt1.setTag(next);
gd.setAdapter(setadap(getarrayList(next, next)));
break;
case 3:
next = (Integer.parseInt(txt1.getTag().toString().trim()) == 12) ? Integer
.parseInt(txt1.getTag().toString().trim()) : Integer
.parseInt(txt1.getTag().toString().trim()) + 1;
txt1.setText(getMonth(next));
txt1.setTag(next);
System.out.println("top" + next + "-"
+ Integer.parseInt(txt.getText().toString().trim()));
gd.setAdapter(setadap(getarrayList(next,
Integer.parseInt(txt.getText().toString().trim()))));
break;
default:
break;
}

}
public class newmyadapter extends BaseAdapter
{
protected ArrayList<String>  data=null;
public newmyadapter(ArrayList<String> data)
{
this.data=data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView txt=new TextView(getContext());
txt.setText(data.get(position));
txt.setGravity(Gravity.CENTER);
txt.setTextColor(Color.BLACK);
String datee=data.get(position)+"-"+Integer.parseInt(txt1.getTag().toString().trim())+"-"+txt.getText().toString().trim();

if(dater!=null)
{
if(dater.contains(datee))

{
txt.setBackgroundColor(Color.RED);
txt.setClickable(false);

}else
{
txt.setBackgroundColor(Color.GREEN);
txt.setOnClickListener(new android.view.View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}
});
}
}
return txt;
}



}

}

Post data in android


try {

URL url = new URL(urls);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot, dest);
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
Progress = "Starting image download...";
publishProgress("");

byte[] buffer = new byte[1024 * 1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
int downloadedSize = bufferLength;

float per = ((float) downloadedSize / totalSize) * 100;

Progress = "Downloading image " + (int) per + "% complete";
publishProgress("");
}

fileOutput.close();
return setImage(dest);

} catch (final MalformedURLException e) {
}
String Proresss=null;